What Does A Symbol (Question Mark) Mean in Scala
What Does A Symbol (Question Mark) Mean in Scala
up vote
10
down vote
favorite
1
I meet some scala code with "?" but do not know what it mean in scala, could
anyone explain it to me ? Thanks.
zjffdu
3,29273663
Can you give an example? The "?" could be part of a method name, class name, or
something else. It's not a standard operator in Scala. Jesper Apr 6 '12 at 6:58
val lovely_? = isItAGoodDay() <-- like that? user166390 Apr 6 '12 at 7:01
is it just me or is the "?" helper method in this case utterly pointless? The same
result, with fewer characters and, IMHO, more clarity, is obtained with standard,
conf.get("foo") getOrElse sys.error("bar") virtualeyes Apr 6 '12 at 9:42
1
@virtualeyes Option.apply(x) converts null to None. So If conf.get("foo") doesn't
return an Option, but might return null, then this is done to convert the possible null
to None (see Christian's answer). Jesper Apr 6 '12 at 11:14
1
Assumed the get in this case was on an Option, but looking again, not the case.
"import Option.{apply => ?}" is, btw, really quite awesome ;-) virtualeyes Apr 6
'12 at 18:41
add a comment
2 Answers
activeoldestvotes
up vote
23
down vote
accepted
For me it looks like the apply method of Option. Is there somewhere the following
import statement in the code:
if conf.get("scoobi.jobid") is not equal null, assign this string, otherwise assign the
string sys.error("Scoobi job id not set.") returns
shareimprove this answer
answered Apr 6 '12 at 8:29
Christian
2,8111724
1
Thanks, you are right. There's an import statement ahead "import Option.{apply =>
?}" zjffdu Apr 6 '12 at 8:39
add a comment
up vote
7
down vote
It's just a legal character, just like "abcd..."
UPD2: In the example "?" could be function, method of this or just some object with
apply method. It probably returns Option[String].