Please enable JavaScript.
Coggle requires JavaScript to display documents.
Swift's enum - the most important language feature (Vague Initial…
Swift's enum - the most important language feature
Vague Initial understanding of the topic
Thought close to C's enums
Mainly used it for grouping
Sundel''s post was a bit eye opening but still
When to use vs structs was not clear at all
Associated values looked nice but didn't quite get it
When first reading the Swift programming guide, did not spend much time on enums
Started learning FP
ADT
Started to shed some light
Elm lang claimed to be the most important feature of the Elm language
One of the first thing they talk about
ADT and Sum Types vs Product Types
Definitions
Inhabitants
Possible values of a type
From kotlin presentation
Debasish Gosh book has a nice definition of Sum Types
Nice explanation of "Sum"
also nice explanation of Product through the associated values of an enum
nice diagram of types A, B to A + B, A *B
The net takeaway from this section is that an ADT forces you to build abstractions strictly according to the rules that you’ve defined for it.
Also nice that he goes straight to example - should I do it this way?
explain why the terms sum and product
From kotlin presentation
Isomorphisms
Display some isomorphisms for reasoning between different structures
George's example where Sum type with same field across different cases vs the field extracted outside into a top level struct
Optional vs Custom type with None case
Enum vs class memory representation would be cool
Samples
Sample Usages in the iOS platform
the Result case
history
See also example from kotlin
presentation
Guess what Optional is also declared as Sum Type (a bit supercharged)
Sample usages form our App
State machines
Models
How exhaustiveness helps in refactoring
Do not use the default!
Show a sample from adding another error case in our app the compilation errors we would get
Typed Errors
Try to log how many enums we have in our app
F#
Feinman elm
From kotlin presentation contact details
Advantages
Precise modeling
Making illegal states unrepresentable
The net takeaway from this section is that an ADT forces you to build abstractions strictly according to the rules that you’ve defined for it. (Debasish Gosh)
elm docs claim that it is the most important feature
Exhaustiveness
Refactoring
readability
You are forced to think about and handle all the different cases from
Sample from typed error handling from autologin
How would you express this in other OOP languages?
Visitor pattern
Thank god we do not have to do this!
There is no nice way to capture that with records,
ShortComings
Manipulation
Change
As nesting level grows...
How to deal with it
Code generation for optional getters
See pointfree snippet
Optics
Sum up
Where to look to gain deeper insight
FP
Glossary
Scala
Sealed traits
Soon enums in Scala 3
Haskell
Data types
F#
Discriminated unions
Kotlin
Sealed class
pattern matching
How is this called across languages (usually pattern matching but verify this)
Swift's nicer than Kotlin and Scala!
Elm
From elm Docs -
Custom types are the most important feature in ElM!
Years of experience dealing with these
Better tools, libraries different mindset
modelling the domain
Optics
PointFree
Resources/References
Books
Posts
When I first saw it didn't get it
now it is the single feature that would hurt the most if was retrieved
Excellent members of the iOS community but other languages/communities might have a lot to offer
Pointfree
SwiftTalk
Sundell
Most things were not invented by Swift
Swift is a wonderful language but many of its great features were battle tested for years in other languages
Line from twitter about modern languages
"Features get added to modern languages but just adding Sum Types would be more useful than most of them"
If you keep one thing from this talk
Next time you are modelling something think
"Is this supposed to be either this or that or this and that"
Think about sum and product types!
Wrong usages from our app - anti patterns
BottomNotificationView
Analytics
When used were product type should be used