Login

tee/Objects

Role composition

The Perl 6 website has this example of role composition:

role Flyer { method fly { say "I'm flying!" } }

class Animal { method speak { say "Some sound" } }

class Bird is Animal does Flyer {
    method speak { say "Chirp!" }
}

Bird.new.speak;  # Chirp!
Bird.new.fly;    # I'm flying!

Here's OCaml in a similar style and outcome:

class virtual flyer = object method fly = print_endline "I'm flying!" end

class animal = object method speak = print_endline "Some sound" end

class bird = object inherit animal inherit flyer
    method speak = print_endline "Chirp!"
  end

let () =
  (new bird)#speak; (* Chirp! *)
  (new bird)#fly    (* I'm flying! *)

With Merlin, looking at the type of a bird object will show it the abbreviation of bird, but if you immediately look a second time the abbreviation will be expanded to < fly : unit; speak : unit>. Interactively you can #show the type:

# let b = new bird;;
val b : bird = <obj>
# #show bird;;
type bird = < fly : unit; speak : unit >
class bird : object method fly : unit method speak : unit end
class type bird = object method fly : unit method speak : unit end

Constraints

type ('a, 'b) transformer = {
  inputs : 'a list;
  outputs : 'b list;
}
  constraint 'a = < typeid : int ; .. >
  constraint 'b = < typeid : int ; .. >