What do you think of the following additional improvement? The model currently specifies both title and body as String types. So in the Submit logic it's possible to accidentally pass the wrong string, such as Question.titleFromString model.body but then if body still happens to pass the validation for title your view will be wrong but give you no errors. To prevent this, create a type alias TitleString = String and then use that in the model instead of plain String. Then Bob will also know that TitleString and BodyString can never be interchanged accidentally. Does that seem like overkill (or have a flaw I'm not seeing)?
That's not really a question about opaque types, it's on the general theme of "catch everything at compile time" which is what's great about Elm!
The issue is that a type alias is just a new name for a type (an alias). Therefore, TitleString is a just new way to say String. This means that Question.titleFromString model.body would still compile, even when using TitleString in your Model and Question API. The compiler would see String everywhere.
We can use an opaque type! Imagine we define a Question.TitleField module with this API:
This module ties a a TitleField value with its form field view and its validation. Finally, we use TitleField in our Model, using blank to initialize it, title for validation, and view to render it!
moduleQuestion.TitleFieldexposing(TitleField,blank,form)importFormtypeTitleField=TitleFieldStringblank:TitleFieldblank=TitleField""form:FormTitleFieldQuestion.Titleform=Form.textField{parser=Question.titleFromString,value=\(TitleFieldvalue)->value,update=\newValueoldValue->TitleFieldnewValue,attributes={label="Question title",placeholder="Type the statement of your question..."}}
I'm glad this led back to opaque types. 8-D Using something like TitleField type is probably too much for the simple example, but a great solution for a site that has to be really robust! hecrj/composable-form looks great, I'm going to check that out too.
As for type aliases... I don't know enough about compiler design to have an informed opinion, but what I described is how I would "want" type aliases to work. I totally understand that after being compiled they're all just Strings (in this example), but if the coder specifies a different name I think I want a step somewhere that will catch that if the wrong name is used. But your TitleField solution is better anyway, it provides a lot more than just a new name for a type. 8-)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Great explanation of opaque types, thank you!
What do you think of the following additional improvement? The model currently specifies both
title
andbody
asString
types. So in theSubmit
logic it's possible to accidentally pass the wrong string, such as Question.titleFromString model.body but then ifbody
still happens to pass the validation fortitle
your view will be wrong but give you no errors. To prevent this, create atype alias TitleString = String
and then use that in the model instead of plainString
. Then Bob will also know that TitleString and BodyString can never be interchanged accidentally. Does that seem like overkill (or have a flaw I'm not seeing)?That's not really a question about opaque types, it's on the general theme of "catch everything at compile time" which is what's great about Elm!
Good question!
The issue is that a
type alias
is just a new name for a type (an alias). Therefore,TitleString
is a just new way to sayString
. This means thatQuestion.titleFromString model.body
would still compile, even when usingTitleString
in yourModel
andQuestion
API. The compiler would seeString
everywhere.We can use an opaque type! Imagine we define a
Question.TitleField
module with this API:This module ties a a
TitleField
value with its form fieldview
and its validation. Finally, we useTitleField
in ourModel
, usingblank
to initialize it,title
for validation, andview
to render it!composable-form can help you write this:
I'm glad this led back to opaque types. 8-D Using something like
TitleField
type is probably too much for the simple example, but a great solution for a site that has to be really robust!hecrj/composable-form
looks great, I'm going to check that out too.As for type aliases... I don't know enough about compiler design to have an informed opinion, but what I described is how I would "want" type aliases to work. I totally understand that after being compiled they're all just
String
s (in this example), but if the coder specifies a different name I think I want a step somewhere that will catch that if the wrong name is used. But yourTitleField
solution is better anyway, it provides a lot more than just a new name for a type. 8-)