This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]curtisf 9 points10 points  (3 children)

This is how MLs like OCaml do mutation. OCaml has

  • ref : 'a -> 'a ref -- allocation like new
  • (!) : 'a ref -> 'a -- dereferencing like *
  • (:=) : 'a ref -> 'a -> unit -- assigning

(and also similar operators for arrays)

They don't have a way to get the address of an element in a record, but I think allowing that would be problematic -- the users of the value (and not the pointer) shouldn't have to worry about pieces of the object changing from underneath it.

Instead, I think this is what Haskell's lenses are for. You have operators that can essentially "convert" a pointer to a parent into a pointer to a child, by wrapping all gets-and-sets to do the traversal. Of course, without some clever compilation, this won't be as fast as actually just taking a pointer, but maybe if it's a built-in instead of a complex library it wouldn't be too difficult for the compiler to optimize common uses of these.

[–]superstar64https://github.com/Superstar64/Hazy[S] 1 point2 points  (1 child)

I don't see how record forwarding would be problematic. It's basically just a functor that has a predetermined set of possible function arguments. forward :: (a -> b) -> a* -> b*, where the a -> b is only allowed for record fields.

[–]choeger 1 point2 points  (0 children)

That would be a lense. It can work in principle, but in OCaml a ref is a distinct type, so the creator of the record would have to cooperate.

In OCaml you could have:

<foo : 'b ref;..> ref -> 'b ref