@michaelmmacleod What is the observed behavior?
(rereading - get it now - the program runs without errors)
The docs say: “External contracts govern behavior when an object is instantiated from a class or when methods or fields are accessed via an object of that class. ”
Also: “Methods and fields listed in an absent clause must not be present in the class.”
So, I think, since class/c
optionally has an absent
clause, but no present
clause, that you won’t get an error when a field is missing from the init
section.
The docs doesn’t mention what happens when a field is missing, though:
An initialization argument contract, tagged with init, describes the expected behavior of the value paired with that name during class instantiation. The same name can be provided more than once, in which case the first such contract in the class/c form is applied to the first value tagged with that name in the list of initialization arguments, and so on.
It’s not unreasonable to expect an error though - but is it possible to detect the error at compile time?
The values bound to initialization variables are
- the arguments provided with instantiate or passed to make-object, if the object is created as a direct instance of the class; or,
- the arguments passed to the superclass initialization form or procedure, if the object is created as an instance of a derived class.
Thanks @soegaard2. I don’t know much at all about the internal representation of objects and classes, but it would make sense to me that this would be the kind of thing that Racket couldn’t detect at compile time. The problem popped up when I removed some initialization fields from a class and forgot to update their contracts—I was surprised that my code compiled when I realized my mistake.