Currently (Babel-17 v0.3.2), how destructing works is not really what I intended initially:
def myobj = object def destruct_ Boom = 1 end
#assert
match myobj
case Boom x => false
case Boom ! x => x == 1
end
#assert
match Boom 1
case Boom x => x == 1
case Boom ! x => false
end
Both assertions currently hold. This is because the pattern Boom x only matches a true cexpr, but not a value that just responds to the destruct_ message.
The thing is that initially I introduced constructors just so that effortless pattern matching is possible against objects that are NOT of type cexpr, but choose to behave as such. Therefore the pattern C pat where C is a constructor should really mean the same as C ! pat. There is only one problem with that:
typedef suit Hearts, Diamonds, Spades, Clubs
This currently is short for
typedef suit (x as Hearts) = x typedef suit (x as Diamonds) = x typedef suit (x as Spades) = x typedef suit (x as Clubs) = x
Now, if I do the following:
def myobj = object def destruct_ Hearts = nil def hello = "Hello there!" end val strangeSuit = suit myobj
Now strangeSuit is of type suit, but also responds to the hello message. That surely wasn’t intended.
The solution is to define the meaning of the suit type definition
to be:
typedef suit Hearts = Hearts typedef suit Diamonds = Diamonds typedef suit Spades = Spades typedef suit Clubs = Clubs
Then everything works fine and the encapsulation of the suit type is guaranteed.