[ nice-Bugs-930132 ] lazy initialization
"SourceForge.net" <[email protected]> Mon, 05 Apr 2004 21:25:29 -0700
| Newsgroups | gmane.comp.lang.nice.devel |
|---|---|
| Message-ID | <[email protected]> |
Bugs item #930132, was opened at 2004-04-06 00:46
Message generated for change (Comment added) made by bsponline
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=112788&aid=930132&group_id=12788
Category: None
Group: None
>Status: Closed
Resolution: None
Priority: 5
Submitted By: Ka-Hing Cheung (bsponline)
Assigned to: Nobody/Anonymous (nobody)
Summary: lazy initialization
Initial Comment:
It's common to do this for lazy initialization:
?Blah blah = null;
if (blah == null)
blah = new Blah();
blah.doThis();
However, this fails because the compiler thinks that
blah maybe null when you call blah.doThis().
If I declare blah as Blah (without ?), then the
compiler complains that I am comparing blah with null
(blah == null).
There should be a way to do this without using a
separate boolean variable to check if blah has been
initialized.
$ nice --version
Nice compiler version 0.9.6 (build 2004.03.05, 14:17:55
UTC)
----------------------------------------------------------------------
>Comment By: Ka-Hing Cheung (bsponline)
Date: 2004-04-06 04:25
Message:
Logged In: YES
user_id=159910
Oops, it doesn't work on fields.
----------------------------------------------------------------------
Comment By: Ka-Hing Cheung (bsponline)
Date: 2004-04-06 02:37
Message:
Logged In: YES
user_id=159910
This also fails:
?Blah blah;
then later:
return blah == null ? "N/A" : blah.getName();
This should also work, but I need to do this to get it to
compile:
return blah == null ? "N/A" : notNull(blah).getName();
----------------------------------------------------------------------
Comment By: Ka-Hing Cheung (bsponline)
Date: 2004-04-06 01:10
Message:
Logged In: YES
user_id=159910
Okay, I now have a way to work around this:
Declare blah as usual:
?Blah blah = null;
Then later:
if (blah == null)
blah = new Blah();
Blah tmp = notNull(blah);
tmp.doThis();
This works, but I think that's not very intuitive, as you
know that blah cannot be null already.
----------------------------------------------------------------------
Comment By: Ka-Hing Cheung (bsponline)
Date: 2004-04-06 00:59
Message:
Logged In: YES
user_id=159910
A way to fix this would be, allow Blah blah to be compared
with null. That way I can modify the first line to:
Blah blah = cast(null);
And do not need to change anything else.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=112788&aid=930132&group_id=12788
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click