-
Notifications
You must be signed in to change notification settings - Fork 3
Présenter Scala
davidB edited this page May 11, 2012
·
5 revisions
Page wiki à complèter
Java | Scala |
```java Map myMap = ... // cas 1 B myB = myMap.get(a); if (myB != null) { doStuff(myB); } ``` ```java // cas 2 B myB = myMap.get(a); if (myB != null) { doStuff(myB); } else { doStuff2(); } ``` ```java // cas 3 B myB = myMap.get(a); C myC = (myB != null) ? doStuff(myB) : doStuff2(); ``` |
```scala
val myMap = ...
// cas 1.1
myMap.get(a).foreach(myB => doStuff(myB))
// cas 1.2
myMap.get(a).foreach(doStuff)
```
```scala
// cas 2.1 (si default de myMap est "null")
myMap(a) match {
case null => doStuff2()
case myB => doStuff(myB)
}
// cas 2.2 myMap.get(a) match { case Some(myB) => doStuff(myB) case None => doStuff2() }
|