On Wednesday, February 15, 2017 at 8:49:13 PM UTC, Rupert Smith wrote:
>
> I am trying to build a model with states, such that fields are only 
> available that are actually needed in the relevant state. The model is like 
> this:
>
> type alias WithPosition a =
>     { a | rect : Rectangle }
>

>
> type alias WithValue a =
>     { a | value : String }
>
>
> type State
>     = Hidden
>     | Aware (WithPosition {})
>     | Active (WithPosition (WithValue {}))
>     | Inactive (WithPosition (WithValue {}))
>

Having played around with this for a while, I don't think this pattern is 
worth continuing with.

It is much easier to use non-extensible records and take advantage of 
tagged union constructors letting you have >1 argument. Like this:

type State
    = Hidden
    | Aware WithPosition
    | Active WithPosition WithValue
    | Inactive WithPosition

(Of course I could just use Rectangle and String directly - but the actual 
model I am working with has more fields)

This avoids the need to unpack and repack record types unnecessarily to try 
and work around the typing issues.

It has been a learning experience to play around with them, but I think 
without an existential qualifier, extensible records are not terribly 
useful at all - except for the simple case where you want to write a 
function that projects the fields of a record onto a sub set. As 
existential qualifier would break type inference and that is too useful - 
so extensible records are best avoided.

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to