Skip to content
davidB edited this page May 11, 2012 · 5 revisions

Page wiki à complèter

A des managers

A des dev java

Comparaison de code

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() }

```scala
// cas 3.1
val myC = myMap.get(a) match {
  case Some(myB) => doStuff(myB)
  case None => doStuff2()
}
// cas 3.2
val myC = myMap.get(a).map(doStuff).getOrElse(doStuff2())
  </td>
</tr>

Liens

Clone this wiki locally