Compiler: programWithFlags : … -> Program Never produces a value of uninhabited type Never

Created on 25 May 2016  ·  4Comments  ·  Source: elm/compiler

The compiler magically provides a flags value of type Never to a program of type Program Never. Most constructors of Program Never automatically ignore this “value”, but a program constructed with programWithFlags can receive it in init and use it to break the safety of APIs expecting Never to be uninhabited.

Demo:

import Html
import Html.App

shouldNeverHappen : Never -> String
shouldNeverHappen n = "Got a value of type Never: " ++ toString n

main : Program Never
main =
  Html.App.programWithFlags
    { init = \flags -> (flags, Cmd.none)
    , update = \_ model -> (model, Cmd.none)
    , subscriptions = \_ -> Sub.none
    , view = \model -> Html.text (shouldNeverHappen model)
    }

displays Got a value of type Never: <internal structure>.

The Never type is supposed to be uninhabited and should not be used for this purpose. A program taking no flags should be marked with type Program (), not Program Never.

Most helpful comment

Or better (I think?), can we just remove the flags parameter from Program entirely, changing

  • Program Never to Program,
  • Program flags to flags -> Program, and
  • programWithFlags { init = \flags -> …, … } to \flags -> program { init = …, … }?

All 4 comments

Or better (I think?), can we just remove the flags parameter from Program entirely, changing

  • Program Never to Program,
  • Program flags to flags -> Program, and
  • programWithFlags { init = \flags -> …, … } to \flags -> program { init = …, … }?

Assuming it's doable, I like how that suggested API would remove the type variable from Program.

Also, I like how it'd let you wire things up differently based on flags. I'd use that feature for webworkers. (Accept a flag for running things in parallel vs not, depending on whether the current browser supports it, then use a different update accordingly.) I can work around it, of course, but it'd be nicer to do it that way. 😄

Interesting, I missed the suggestion about flags -> Program. That seems nice and we have to mess with the Program type for 0.18 anyway.

Well, either way, it is fixed in 0.18 right now, but it may make sense to switch to the function way.

I missed the suggestion about flags -> Program. That seems nice and we have to mess with the Program type for 0.18 anyway.

I totally ❤️ that design. I would love to see that be a part of 0.18 if it's not too late!

Was this page helpful?
0 / 5 - 0 ratings