var foo = 1;
var output = 'Output: ';
switch (foo) {
case 10:
output += 'So ';
case 1:
output += 'What ';
output += 'Is ';
case 2:
output += 'Your ';
case 3:
output += 'Name';
case 4:
output += '?';
console.log(output);
break;
case 5:
output += '!';
console.log(output);
break;
default:
console.log('Please pick a number from 0 to 6!');
}
Read more:Stackoverflow:Switch multiple cases in JS
switch-statement break-keyword multi-case multi-case-chained operation
package quizful {
package prof {
class Executive {
private[prof] var details = null
private[quizful] var friends = null
private[this] var secrets = null
def help(another : Executive) {
println(another.details)
println(another.secrets) //ERROR
}
}
}
}
Note the following points:access modifiers qualifiers
import p._ // all members of p (this is analogous to import p.* in Java).
import p.x // the member x of p.
import p.{x => a} // the member x of p renamed as a.
import p.{x, y} // the members x and y of p.
import p1.p2.z // the member z of p2, itself member of p1.
Furthermore, the clause import p1._, p2._ is a shorthand for import p1._; import p2._.
Implicitly imported into every compilation unit are, in that order:import
names.mkString(",")
// is the same as
names mkString ","
This syntax is formally known as “infix notation”. It should only be used for purely-functional methods (methods with no side-effects) - such as mkString -or methods which take functions as parameters - such as foreach:
// right!
names foreach (n => println(n))
names mkString ","
optStr getOrElse ""
// wrong!
javaList add item
Read more: Scala documentation: Method Invocation
infix notation arity-1
def apply(f: Int => String, v: Int) = f(v)
Note: methods are automatically coerced to functions if the context requires this.
class Decorator(left: String, right: String) {
def layout[A](x: A) = left + x.toString() + right
}
object QuizfulTest extends App {
def apply(f: Int => String, v: Int) = f(v)
val decorator = new Decorator("[", "]")
println(apply(decorator.layout, 2015))
}
Execution yields the output:
[2015]
In this example, the method decorator.layout is coerced automatically to a value of type Int => String as required by method apply. Please note that method decorator.layout is a polymorphic method (i.e. it abstracts over some of its signature types) and the Scala compiler has to instantiate its method type first appropriately.
higher-order functions polymorphic method
unit return type
scala> def doTwoTimes(fn: (Int) => Unit) = {
| fn(1); fn(2);
| }
doThreeTimes: ((Int) => Unit)Unit
scala> doTwoTimes(println)
1
2
scala> def specialPrint(num: Int) = {
| println(">>>" + num + "<<<")
| }
specialPrint: (Int)Unit
scala> doTwoTimes(specialPrint)
>>>1<<<
>>>2<<<
>>>3<<<
In the definition of doTwoTimes we specify that the method takes a parameter called fn, which has a type of (Int) => Unit. This means that fn is a method that takes a single parameter of type Int and a return type of Unit, which is to say fn isn’t supposed to return a value at all just like a Java void function.
void
def f(x: Option[Int]) = (x: @unchecked) match {
case Some(y) => y
}
Without the @unchecked annotation, a Scala compiler could infer that the pattern match is non-exhaustive, and could produce awarning because Option is a sealed class.
sealed class match expression pattern matching