What is the use of general patterns for `self` in class definitions?

Dear community,

a colleague of mine made me notice that in class definitions, the AST construct for self is a pattern, as opposed to an (optional) variable. To be clearer, in the declaration:

class c = object (self) end

self is considered as a pattern. And indeed, I can write, for example

class d = object (((_ as self) | self) as self2) end

which is legit OCaml code.

I fail to see, however, how other forms of patterns (tuples, variants, etc) can be used.
I am pretty sure I am missing something here.

Hence the question: why do we need such a general pattern construct for self?

NB: I am not asking for any changes. I am just being curious.

One use I know is that it allows the introduction of type annotations:

class c = object (self : #foo) ... end

or

class c = object (_ : 'self) constraint 'self = .... end

etc.

Cheers,
Nicolas

1 Like

You’re right. Thanks.
I am still interested in more examples, that make use of patterns that really deconstruct values.