initialize instance variables by using optional parameter

16 views
Skip to first unread message

yutopp

unread,
May 14, 2012, 6:53:58 AM5/14/12
to General Dart Discussion
Hello.
I have a question with code below.
If same question has been posted, I'm sorry.
I'm using DartEditor build 7566, and it runs as Server Application.

class hoge
{
hoge([final int this.number]);
final int number = 100;
}

void main() {
print( new hoge().number ); // I want this to print 100,
but it prints null.
print( new hoge( 200 ).number ); // This prints 200.
}

I understand the value of non-initialized variables is null, but in
this case, I think value of hoge.number should be 100.
Thanks.

William Hesse

unread,
May 14, 2012, 7:13:02 AM5/14/12
to yutopp, General Dart Discussion
This is issue 2160 on the Dart issue tracker.
http://code.google.com/p/dart/issues/detail?id=2160

The resolution of this issue is uncertain.
The suggested fix is
class hoge {
hoge([this.number = 100]);
final int number;
--
William Hesse
Software Engineer
whe...@google.com

Google Denmark ApS
Frederiksborggade 20B, 1 sal
1360 København K
Denmark
CVR nr. 28 86 69 84

If you received this communication by mistake, please don't forward it
to anyone else (it may contain confidential or privileged
information), please erase all copies of it, including all
attachments, and please let the sender know it went to the wrong
person. Thanks.

yutopp

unread,
May 14, 2012, 7:22:15 AM5/14/12
to General Dart Discussion
Thank you very much.

Regards,
yutopp
Reply all
Reply to author
Forward
0 new messages