Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a "?" symbol (question mark) mean in Scala?

Tags:

syntax

scala

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."))
like image 946
zjffdu Avatar asked Apr 06 '12 06:04

zjffdu


People also ask

What is the use of question mark in Scala?

Asking for help, clarification, or responding to other answers.

What is the use of question mark in Scala Mcq?

The syntax of using three question marks in Scala lets you write a not-yet implemented method, like this: def createWorldPeace = ??? The methods you define can also take input parameters and specify a return type, like this: def doSomething(s: String): Int = ???

What does _* mean in Scala?

: _* is a special instance of type ascription which tells the compiler to treat a single argument of a sequence type as a variable argument sequence, i.e. varargs.

What does putting a question mark after a parameter name DP?

Question mark means "optional". So it's a shorthand for "parameter: type | undefined = undefined"..


2 Answers

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

like image 127
Christian Avatar answered Oct 10 '22 05:10

Christian


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

like image 25
senia Avatar answered Oct 10 '22 05:10

senia