[nem-en] Field initializers specification
Kamil Skalski
kamil.skalski at gmail.com
Fri Mar 16 19:49:24 CET 2007
>
> Currently we compile even "base();base();" without any warning/error.
> Extension compared to c# can be for example "this(x){def smth=g(x); if
> (smth) base(); else this(2);}"
> So recursive ctor can be created :))
Yeah... http://nemerle.org/bugs/view.php?id=664
>
> I suppose something like this will be acceptable:
> 0) wait until all macros are run, or more exactly until class won't change
> in future (also wait for default c-tor generation)
The current place where this is handled currently is Typer
MaybeAddBaseCall (m : MethodBuilder) : void
This is good place, but the problem is that this code is quite simplistic.
> 1) if some c-tor doesn't have calls to base or this, inject base() to it's
> the beginning
Yes, we do this currently - just the detection is very simple - we
traverse the top level sequence, not going deep into expressions.
> 2) replace all calls to base with field_init; base(..) (so initializer
> macro will be used only to generate field_init, but not to inject it)
This is a nice idea - I guess making StaticInitializer and
MaybeAddBaseCall cooperate is a good idea. We could use some existing
special TExpr node or just insert some plain parsed node, like
_N_init (...) which will be consumed by MaybeAddBaseCall (we do
similar things for checked/unchecked blocks, etc.)
> And additional things:
> 3) ctor can have default parameters (currently it can)
I'm not against this feature. Especially if it works now (to be sure
it won't stop, it's always best to commit some tests to the suite :)
> 4) do some execution flow check to ensure that base/this was called exactly
> once in each ctor body (and recursion is ok, but must be reported as
> warning). Emit warnings/errors if we can't be sure about this.
I'm not sure PEVerify will allow this to happen. And I'm not sure if
this is worth complicating - we don't have a good flow analysis
pattern at the moment, so making it work will indeed be tricky.
> 5) maybe do same runtime checks
> 6) check access to "this" before base/this is called. Do a warning at
> least. Field init is before "base()", so "this" references will be reported.
I think it can be done by using a simple flag in Typer, like
'ctor_call_occured' - it should be easy to handle, especially if we
only allow ctor calls at top level sequence.
>
> Some things are tricky to implement, but must be specified at least :)
>
I think that most of this stuff is possible to implement without great
amount of work or knowledge... so any help is welcome :)
--
Kamil Skalski
http://nazgul.omega.pl
More information about the devel-en
mailing list