Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cmd with no message in Elm

Is it possible to create a Cmd that sends no message on completion in Elm?

Specifically, I am trying to have an element grab the focus (programmically), but I don't need to be informed of the result:

Dom.focus "element-id"
    |> Task.attempt FocusReceived
...
FocusReceived result ->
    model ! []  -- result ignored

Is there any way to just have the Elm "engine" not send a message after this Cmd?

I know that my code (FocusReceived result -> model ! []) is a no-op, but I would like the message not to be sent at all.

like image 371
Ralph Avatar asked Jan 21 '18 13:01

Ralph


1 Answers

No, a Msg is always required. It is a common idiom in typical Elm projects to include a Msg type constructor that does nothing named NoOp.

type Msg
    = NoOp
    | ...

The update function does what FocusReceived in your example does, namely, nothing.

case msg of
    NoOp ->
        model ! []
    ...
like image 105
Chad Gilbert Avatar answered Nov 20 '22 02:11

Chad Gilbert