instance initializer

Peter Levart peter.levart at gmail.com
Sun Oct 6 16:51:41 UTC 2019


On Sunday, October 6, 2019 6:21:45 PM CEST Brian Goetz wrote:
> > In these scheme, canonical constructor also acts as instance initializer,
> > since it is always called from other constructors. Classical instance
> > initializer is therefore not needed any more and could be prohibited in
> > record types.
> 
> I would agree that instance initializers in records are mostly useless,
> and keeping them around adds some complexity.  Any work that can be done
> in an II could also be done in a compact ctor with about the same number
> of keystrokes:
> 
>      { ++instanceCount; }
> 
> vs
> 
>     Foo { ++instanceCount; }
> 
> The argument for keeping them is to minimize the number of gratuitous
> differences between records and classes.  But, "it is a compile-time
> error for a record class to have an instance initializer" is a pretty
> simple spec change... and probably no one will notice.

I know that making special rules in record constructors (about being able to 
access instance fields) is also increasing the number of gratuitous 
differences between records and classes, but in order to ban instance 
initializers, there has to be a an alternative way to specify initializing 
code that is always executed. Perhaps it is enough just to suggest users to 
put such code into canonical constructor and always call that constructor from 
other constructors as opposed to forcing them to do that with language 
constraints.

Regards, Peter





More information about the amber-spec-experts mailing list