Download - Unrepresentable states in Elm
![Page 1: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/1.jpg)
Elm and unrepresentable states
My Tailor-made Model
Nicolas Umiastowski@n_umiastowski
Remote meetup November 22nd 2016
![Page 2: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/2.jpg)
![Page 3: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/3.jpg)
![Page 4: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/4.jpg)
First attempt : make it workIn a very « non-functional » kind of way
![Page 5: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/5.jpg)
13
2
1
0
![Page 6: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/6.jpg)
Update
![Page 7: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/7.jpg)
Answered questions
There questions are really answered?
![Page 8: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/8.jpg)
![Page 9: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/9.jpg)
I want to be able to make sure that:
If one question is answered, then it has an answerId
Answered question that does not have an answerId : Possible
Invalid states?
![Page 10: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/10.jpg)
Unrepresentable state:
Encoding the answered state of a question in the constructor means that I can’t write code that doesn’t check for whether a question is
answered or not
![Page 11: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/11.jpg)
Making undesired state unrepresentable
![Page 12: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/12.jpg)
Distinguish between answered and non answered questions.
But that is not a state, that is just two lists!
![Page 13: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/13.jpg)
Unrepresentable states
I display only one list of questions
I check that there is no unanswered questions
![Page 14: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/14.jpg)
What could happen… ?
![Page 15: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/15.jpg)
I could have answered questions without an answerId
![Page 16: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/16.jpg)
We can check if a questions has an answerId or not, but we cannot check that it has an “answered” or “unanswered” state.
Invalid states?
![Page 17: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/17.jpg)
Define a real state for the questions
![Page 18: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/18.jpg)
![Page 19: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/19.jpg)
![Page 20: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/20.jpg)
![Page 21: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/21.jpg)
Modify an existing answer
Answer a question
![Page 22: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/22.jpg)
If there are only answered questions….
… then we display the messsage
![Page 23: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/23.jpg)
All questions have only two possible states : • Answered• Unanswered
Invalid states?
![Page 24: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/24.jpg)
![Page 25: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/25.jpg)
![Page 26: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/26.jpg)
3rd attempt of unrepresentable statesWhy not have a state for the answer, instead of an Id ?
![Page 27: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/27.jpg)
The answers also have different states.
![Page 28: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/28.jpg)
State of the question
State of the answer
![Page 29: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/29.jpg)
By the way… unrepresentable states does not prevent all impossible states!
![Page 30: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/30.jpg)
I could have two different questions with the same Id, one question with an empy text, etc…
![Page 31: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/31.jpg)
ConclusionAlways try to see if a good pattern can be appliedIt will help you learn and get betterTakes more time at the beginning, but then we go faster and bugs are avoided
![Page 32: Unrepresentable states in Elm](https://reader036.vdocuments.site/reader036/viewer/2022081605/58ee97041a28ab5e0e8b45e9/html5/thumbnails/32.jpg)
Questions ?