Constraint clause in class

Hi,

class material = object
    method glow c = Printf.sprintf "highlighting with color %s" c
end ;;

class ['a] mesh (m : 'a) = object
    constraint 'a = #material
    val mutable material = m   
    method highlight = material#glow
end ;;

let m = new mesh (new material) ;;
m#highlight "blue" ;;

The above code works if constraint 'a = #material or constraint 'a = material.
Does the above constraint clause have the same meaning with or without β€œ#” ?

Thanks.

No, #material is an abbreviation for the type of any object implementing the signature of material:

< glow : string -> string; .. >

On the other hand, material is an abbreviation for the exact type of the material class, namely:

< glow: string -> string >

For example, you can do

new mesh (object method glow _ = "hola" method foo = 42 end)

if you use constraint 'a = #material but not if you use constraint 'a = material as the argument has an β€œextra” method foo that does not appear in the signature of the class material.

Cheers,
Nicolas

6 Likes