Hacker News new | past | comments | ask | show | jobs | submit login

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}



F# 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}


Interesting, 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)))




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: