OO emulated by ML with record concatenation

Started by
2 comments, last by twanvl 17 years, 9 months ago
Hi guys, I'm currently reading a lot about programming language and I’m trying to find a small subset of functionalities I would like to add to a new ML programming language and I'm wondering what has happened to "record concatenation". It seems to me a way to merge OO (maybe more prototype oriented programming) and ML. Nevertheless no research seems to go in this direction... I'm wondering why! For example, one could write: let dog = { weight = 10; bark = (fun () -> print "Waouf\n") } let mydog = dog with { weight = 2; tiny = true } In this case, weight is overridden and tiny is a new field. If someone has a clue of why this kind of functionalities is not used, please tell me! Many thanks [Edited by - cinag on July 9, 2006 7:06:32 AM]
Advertisement
Or if someone has a set of functionalities that give the encapsulation power of object with minimum modification of the ML language and without any complex construct.
What you propose would require an overhauling of the ML typing system, which supports generalization but not supertypes. That system was introduced in O'Caml for instance (albeit with a slightly different syntax) :

class dog = object  method weight = 10  method bark () = print_endline "Waouf"endclass mydog = object  inherit dog  method weight = 2  method tiny = trueend


The problem here is that you will need to introduce a way to represent the actual fields. In ML, records are identified by their fields, so using the field weight would identify one and only one type (which?). To work around this, O'Caml introduces two parallel type schemes. The first is the anonymous class system:

< bark : unit -> unit; weight : int >

That is, a class is represented only by its public members. It gets somewhat tricky to use and you need to perform the casting manually (because supertyping might cause you to lose information). The alternative is class-name-based typing: you give names to such types, and allow conversions through the inherit keyword. In this case, mydog and dog are two types, and mydog can be supertyped into dog, which can be supertyped into
< bark : unit -> unit; weight : int >.

Note that the supertying operator :> does not exist in ML at all, and was introduced to manage supertypes correctly.

Also, check this text out.
You should check out the paper "Extensible records with scoped labels". Altough it uses haskell, the concept should be the same for ML.

This topic is closed to new replies.

Advertisement