0% found this document useful (0 votes)
37 views3 pages

What Does A Symbol (Question Mark) Mean in Scala

The '?' symbol in Scala code represents the apply method of the Option object, which is imported as '?' . The apply method converts a null value to None if the argument is null, otherwise it returns Some containing the argument. In the example, '?' is checking if a configuration value is null and if so throws an error, otherwise returns the configuration value.

Uploaded by

larrynina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views3 pages

What Does A Symbol (Question Mark) Mean in Scala

The '?' symbol in Scala code represents the apply method of the Option object, which is imported as '?' . The apply method converts a null value to None if the argument is null, otherwise it returns Some containing the argument. In the example, '?' is checking if a configuration value is null and if so throws an error, otherwise returns the configuration value.

Uploaded by

larrynina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

And here's one example

def getJobId(conf: Configuration): String =


?(conf.get("scoobi.jobid")).getOrElse(sys.error("Scoobi job id not set."))
scala syntax
shareimprove this question
edited Apr 6 '12 at 7:12

asked Apr 6 '12 at 6:57

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:

import Option.{apply => ?}


This means apply is imported as ?. From the doc of Option.apply:

An Option factory which creates Some(x) if the argument is not null,


and None if it is null.
The whole statement means then:

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..."

scala> def ?(i: Int) = i > 2


$qmark: (i: Int)Boolean

scala> val a_? = ?(3)


a_?: Boolean = true
UPD: See Valid identifier characters in Scala , Scala method and values names

UPD2: In the example "?" could be function, method of this or just some object with
apply method. It probably returns Option[String].

You might also like