Discussion:
"local variables must be initialized"
Paul Phillips
2008-06-30 17:33:09 UTC
Permalink
Is it a bug that this:

var x: String = _

works nicely unless I'm in a try/catch block, in which case
it fails with "error: local variables must be initialized."
This works:

var x: String = null

This however is undesirable for my task, which demands
maximum genericity - i.e. now I have to handle whatever
cases, var i: Int = 0 since null won't fly, etc.
--
Paul Phillips | Every normal man must be tempted at times
Analgesic | to spit on his hands, hoist the black flag,
Empiricist | and begin to slit throats.
i'll ship a pulp | -- H. L. Mencken
Jan Kriesten
2008-07-01 05:22:19 UTC
Permalink
Hi Paul,
Post by Paul Phillips
var x: String = null
This however is undesirable for my task, which demands
maximum genericity - i.e. now I have to handle whatever
cases, var i: Int = 0 since null won't fly, etc.
you might want to try:

var x: String = x

Best regards, --- Jan.
Viktor Klang
2008-07-01 07:34:40 UTC
Permalink
Post by Jan Kriesten
Hi Paul,
Post by Paul Phillips
var x: String = null
This however is undesirable for my task, which demands
maximum genericity - i.e. now I have to handle whatever
cases, var i: Int = 0 since null won't fly, etc.
var x: String = x
*vomit*
Post by Jan Kriesten
Best regards, --- Jan.
--
Viktor Klang
Rogue Software Architect
Jan Kriesten
2008-07-01 09:01:23 UTC
Permalink
Hi Viktor,
Post by Jan Kriesten
var x: String = x
*vomit*
your comment are really helpful as a solution...

There _are_ case where initializing vars like this is the only option (e.g. when
injection is handled in base classes and setting vars to null would overwrite
the injected value).

If you're ill, you should go to bed, else: any better solution regarding the
problem?

Best regards, --- Jan.
Viktor Klang
2008-07-01 09:10:44 UTC
Permalink
Post by Jan Kriesten
Hi Viktor,
var x: String = x
Post by Viktor Klang
*vomit*
your comment are really helpful as a solution...
There _are_ case where initializing vars like this is the only option (e.g.
when injection is handled in base classes and setting vars to null would
overwrite the injected value).
If you're ill, you should go to bed, else: any better solution regarding
the problem?
Alright, lemme just explain my comment:

There are many occasions where we as software developers actually have a
kind of responsibility not to work around problems, but to actually solve
them.
This is such a case.

"var x : String = x" is not only ugly, it's utterly insane to try to explain
it to a newbie.

While it "solves" the problem for now, we should really try to propose a
change of the syntax instead.

"var s : String = _" is surely okay as syntax, as long as the mechanism is
properly guarded.

My intent was never to offend,

Best regards,
Viktor
Post by Jan Kriesten
Best regards, --- Jan.
--
Viktor Klang
Rogue Software Architect
Viktor Klang
2008-07-01 09:21:42 UTC
Permalink
Could you explain why you want a variable to hold null?
Well someone's gotto hold the null.
But I guess you directed the question to the OP.
Post by Viktor Klang
Post by Jan Kriesten
Hi Viktor,
var x: String = x
Post by Viktor Klang
*vomit*
your comment are really helpful as a solution...
There _are_ case where initializing vars like this is the only option
(e.g. when injection is handled in base classes and setting vars to null
would overwrite the injected value).
If you're ill, you should go to bed, else: any better solution regarding
the problem?
There are many occasions where we as software developers actually have a
kind of responsibility not to work around problems, but to actually solve
them.
This is such a case.
"var x : String = x" is not only ugly, it's utterly insane to try to
explain it to a newbie.
While it "solves" the problem for now, we should really try to propose a
change of the syntax instead.
"var s : String = _" is surely okay as syntax, as long as the mechanism is
properly guarded.
My intent was never to offend,
Best regards,
Viktor
Post by Jan Kriesten
Best regards, --- Jan.
--
Viktor Klang
Rogue Software Architect
--
Viktor Klang
Rogue Software Architect
Ricky Clarkson
2008-07-01 09:22:28 UTC
Permalink
Feel free to provide a reason if you think you have one.
Post by Viktor Klang
Could you explain why you want a variable to hold null?
Well someone's gotto hold the null.
But I guess you directed the question to the OP.
Post by Viktor Klang
Post by Jan Kriesten
Hi Viktor,
var x: String = x
Post by Viktor Klang
*vomit*
your comment are really helpful as a solution...
There _are_ case where initializing vars like this is the only option
(e.g. when injection is handled in base classes and setting vars to null
would overwrite the injected value).
If you're ill, you should go to bed, else: any better solution regarding
the problem?
There are many occasions where we as software developers actually have a
kind of responsibility not to work around problems, but to actually solve
them.
This is such a case.
"var x : String = x" is not only ugly, it's utterly insane to try to
explain it to a newbie.
While it "solves" the problem for now, we should really try to propose a
change of the syntax instead.
"var s : String = _" is surely okay as syntax, as long as the mechanism
is properly guarded.
My intent was never to offend,
Best regards,
Viktor
Post by Jan Kriesten
Best regards, --- Jan.
--
Viktor Klang
Rogue Software Architect
--
Viktor Klang
Rogue Software Architect
Viktor Klang
2008-07-01 09:25:15 UTC
Permalink
Let's await Mr Phillips use-case, and then we can dissect it.
Post by Ricky Clarkson
Feel free to provide a reason if you think you have one.
Post by Viktor Klang
Could you explain why you want a variable to hold null?
Well someone's gotto hold the null.
But I guess you directed the question to the OP.
On Tue, Jul 1, 2008 at 11:01 AM, Jan Kriesten <
Post by Jan Kriesten
Hi Viktor,
var x: String = x
Post by Viktor Klang
*vomit*
your comment are really helpful as a solution...
There _are_ case where initializing vars like this is the only option
(e.g. when injection is handled in base classes and setting vars to null
would overwrite the injected value).
If you're ill, you should go to bed, else: any better solution
regarding the problem?
There are many occasions where we as software developers actually have a
kind of responsibility not to work around problems, but to actually solve
them.
This is such a case.
"var x : String = x" is not only ugly, it's utterly insane to try to
explain it to a newbie.
While it "solves" the problem for now, we should really try to propose a
change of the syntax instead.
"var s : String = _" is surely okay as syntax, as long as the mechanism
is properly guarded.
My intent was never to offend,
Best regards,
Viktor
Post by Jan Kriesten
Best regards, --- Jan.
--
Viktor Klang
Rogue Software Architect
--
Viktor Klang
Rogue Software Architect
--
Viktor Klang
Rogue Software Architect
Ricky Clarkson
2008-07-01 09:20:09 UTC
Permalink
Could you explain why you want a variable to hold null?
Post by Viktor Klang
Post by Jan Kriesten
Hi Viktor,
var x: String = x
Post by Viktor Klang
*vomit*
your comment are really helpful as a solution...
There _are_ case where initializing vars like this is the only option
(e.g. when injection is handled in base classes and setting vars to null
would overwrite the injected value).
If you're ill, you should go to bed, else: any better solution regarding
the problem?
There are many occasions where we as software developers actually have a
kind of responsibility not to work around problems, but to actually solve
them.
This is such a case.
"var x : String = x" is not only ugly, it's utterly insane to try to
explain it to a newbie.
While it "solves" the problem for now, we should really try to propose a
change of the syntax instead.
"var s : String = _" is surely okay as syntax, as long as the mechanism is
properly guarded.
My intent was never to offend,
Best regards,
Viktor
Post by Jan Kriesten
Best regards, --- Jan.
--
Viktor Klang
Rogue Software Architect
Paul Phillips
2008-07-01 10:47:17 UTC
Permalink
Post by Jan Kriesten
var x: String = x
"error: forward reference extends over definition of variable x"
var x: String = x
Post by Jan Kriesten
While it "solves" the problem for now, we should really try to propose
a change of the syntax instead.
Are you guys seeing that syntax work? Not for me, inside or outside
try/catch, on 2.7.1 final.
Post by Jan Kriesten
"var s : String = _" is surely okay as syntax, as long as the
mechanism is properly guarded.
...but back to the original question, it doesn't work inside try/catch.
I'm not sure what you mean by properly guarded.
Post by Jan Kriesten
Could you explain why you want a variable to hold null?
I am trying to faithfully capture the semantics of another environment.
--
Paul Phillips | We must respect the other fellow's religion, but only
In Theory | in the sense and to the extent that we respect his
Empiricist | theory that his wife is beautiful and his children smart.
pal, i pill push | -- H. L. Mencken
Ricky Clarkson
2008-07-01 10:52:16 UTC
Permalink
Post by Paul Phillips
I am trying to faithfully capture the semantics of another environment.
If the other environment was Python, would you emulate dynamic typing when
porting to Scala?
Post by Paul Phillips
--
Paul Phillips | We must respect the other fellow's religion, but only
In Theory | in the sense and to the extent that we respect his
Empiricist | theory that his wife is beautiful and his children smart.
pal, i pill push | -- H. L. Mencken
Landei
2008-07-01 18:42:40 UTC
Permalink
Post by Paul Phillips
var x: String = null
This however is undesirable for my task, which demands
maximum genericity - i.e. now I have to handle whatever
cases, var i: Int = 0 since null won't fly, etc.
Why is
var x: Option[String] = None
no option (pun intended)?
--
View this message in context: http://www.nabble.com/%22local-variables-must-be-initialized%22-tp18200857p18222586.html
Sent from the Scala - User mailing list archive at Nabble.com.
Paul Phillips
2008-07-01 19:11:56 UTC
Permalink
Post by Landei
var x: Option[String] = None
no option (pun intended)?
Because var x: Int doesn't equal none. For reasons that will become
apparent when I release this, I need to limit the special casing as much
as possible.

I appreciate the many replies hypothesizing workarounds but to be honest
I'm only interested in whether it's a bug that _ assignment doesn't work
in try/catch, or if it's not a bug, why this inconsistency shouldn't
surprise me.
--
Paul Phillips | It's better to have gloved and tossed than never to
Moral Alien | have played baseball.
Empiricist |
ha! spill, pupil |----------* http://www.improving.org/paulp/ *----------
Loading...