'Rupert Smith' via Elm Discuss
2017-08-14 14:11:40 UTC
I'm curious if it's possible to use extensible records for HTML attributes
instead of lists of function application such as [id "foo", class "bar"]?
I've poked around a bit, but didn't find too much on specifically this.
let x = anchor { href = "#", id = Just "root" }
Something along the lines of defining common attributes all being maybe
types, and then combining it with a record passed to a function.
type alias CommonAttrs a =
{ a | id : Maybe String, class : Maybe String }
commonAttrs : a -> CommonAttrs a
commonAttrs x =
{ x | id = Nothing, class = Nothing }
anchor : { a | href : String } -> CommonAttrs { href : String }
anchor attrs =
commonAttrs attrs
I know that's not going to work, but something along those lines. I'm new
to Elm and only somewhat familiar with Haskell.
Thoughts?
Extensible records are not as uable as they might at first seem. The reasoninstead of lists of function application such as [id "foo", class "bar"]?
I've poked around a bit, but didn't find too much on specifically this.
let x = anchor { href = "#", id = Just "root" }
Something along the lines of defining common attributes all being maybe
types, and then combining it with a record passed to a function.
type alias CommonAttrs a =
{ a | id : Maybe String, class : Maybe String }
commonAttrs : a -> CommonAttrs a
commonAttrs x =
{ x | id = Nothing, class = Nothing }
anchor : { a | href : String } -> CommonAttrs { href : String }
anchor attrs =
commonAttrs attrs
I know that's not going to work, but something along those lines. I'm new
to Elm and only somewhat familiar with Haskell.
Thoughts?
for this is that they are not the same thing as sub-types and sub-types
will never feature in a language with full type inference, like Elm,
because they present an undecidable problem. I explored this topic in
detail in this thread:
https://groups.google.com/forum/#!searchin/elm-discuss/extensible$20rupert%7Csort:relevance/elm-discuss/NVoWA0ZYZNM/sKDmIP_IBwAJ
The general advice is, do not use extensible records for data modelling in
Elm. It can sometimes be convenient to write functions that restrict their
input to a sub-set of fields in a record; it restricts the dependency of
the function onto the smallest record type that satisfies it, which is a
good use modular design principles. Richard Feldman gave some good examples
in this talk:
--
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 elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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 elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.