Search code examples
elmfeedbackno-op

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.


Solution

  • 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 ! []
        ...