init
is a custom syntax used as if it is an operator to initialize a field/object without the need to pass in the type of the said field/object.
The usual way of initializing:
{ StrBuf buf; pln(#buf); buf = new StrBuf("A"); pln(#buf); buf = StrBuf("B"); pln(#buf); StrBuf foo("C"); pln(#foo); }
Output:
buf=null buf=A buf=B foo=C
Using init
:
{ StrBuf buf; init buf("init"); pln(#buf); }
Output:
buf=init
There is also a conditional init, which allows you to replace this:
if (!buf) init buf();
With this:
init? buf();
Comments
0 comments
Please sign in to leave a comment.