36 ms·
Not exactly comparable (because everything is immutable), but Haskell has sort of a cool approach to setting and getting fields. Let's say you have data P
by wyager 12y ago
Not exactly comparable (because everything is immutable), but Haskell has sort of a cool approach to setting and getting fields.
Let's say you have
data Point = Point {x :: Int, y :: Int}
If you have a Point object called "point", you can get x by doing
x_coord = x point
If you want to create a new Point with x set to 5, you can do
point' = point {x = 5}
- vishbar 12y agoF# has a similar concept via the with statement. To use your example: type point = { x: int; y:int} let z = {x = 5; y = 10} let y = {z with x = 17}
- virtualwhys 12y agoInteresting, Scala equivalent is: case class Point(x: Int, y: Int) val p1 = Point(5,10) val p2 = p1.copy(x=17) How does F# handle nested data types? In Scala if you want to copy a case class of a case class (of a case class ...) it can get boilerplate-y as you have to copy outside-in through each property a la: x.copy(acase.copy(bcase.copy(cprop=1)))