Difference between revisions of "Scala"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) |
PeterHarding (talk | contribs) |
||
| Line 61: | Line 61: | ||
==Default initializer== | ==Default initializer== | ||
var x: String = _ // unloved syntax may be eliminated | var x: String = _ // unloved syntax may be eliminated | ||
=Other= | |||
* https://alvinalexander.com/scala/how-to-use-scala-match-expression-like-switch-case-statement/ | |||
=Match Expressions= | |||
* https://docs.scala-lang.org/overviews/scala-book/match-expressions.html | |||
<pre> | |||
def getClassAsString(x: Any): String = x match { | |||
case s: String => s + " is a String" | |||
case i: Int => "Int" | |||
case f: Float => "Float" | |||
case l: List[_] => "List" | |||
case p: Person => "Person" | |||
case _ => "Unknown" | |||
} | |||
</pre> | |||
<pre> | |||
def isTrue(a: Any) = a match { | |||
case 0 | "" => false | |||
case _ => true | |||
} | |||
</pre> | |||
<pre> | |||
// Version 1 - compiles to a tableswitch | |||
import scala.annotation.switch | |||
class SwitchDemo { | |||
val i = 1 | |||
val x = (i: @switch) match { | |||
case 1 => "One" | |||
case 2 => "Two" | |||
case _ => "Other" | |||
} | |||
} | |||
</pre> | |||
<pre> | |||
</pre> | |||
<pre> | |||
</pre> | |||
<pre> | |||
</pre> | |||
<pre> | |||
</pre> | |||
[[Category:Scala]] | [[Category:Scala]] | ||
Revision as of 10:30, 13 May 2021
Scala Notes
Use of '_'
From - https://stackoverflow.com/questions/8000903/what-are-all-the-uses-of-an-underscore-in-scala
import scala._ // Wild card -- all of Scala is imported
import scala.{ Predef => _, _ } // Exception, everything except Predef
def f[M[_]] // Higher kinded type parameter
def f(m: M[_]) // Existential type
_ + _ // Anonymous function placeholder parameter
m _ // Eta expansion of method into method value
m(_) // Partial function application
_ => 5 // Discarded parameter
case _ => // Wild card pattern -- matches anything
val (a, _) = (1, 2) // same thing
for (_ <- 1 to 10) // same thing
f(xs: _*) // Sequence xs is passed as multiple parameters to f(ys: T*)
case Seq(xs @ _*) // Identifier xs is bound to the whole matched sequence
var i: Int = _ // Initialization to the default value
def abc_<>! // An underscore must separate alphanumerics from symbols on identifiers
t._2 // Part of a method name, such as tuple getters
1_000_000 // Numeric literal separator (Scala 2.13+)
So...
Existential types
def foo(l: List[Option[_]]) = ...
Higher kinded type parameters
case class A[K[_],T](a: K[T])
Ignored variables
val _ = 5
Ignored parameters
List(1, 2, 3) foreach { _ => println("Hi") }
Ignored names of self types
trait MySeq { _: Seq[_] => }
Wildcard patterns
Some(5) match { case Some(_) => println("Yes") }
Wildcard patterns in interpolations
"abc" match { case s"a$_c" => }
Sequence wildcard in patterns
C(1, 2, 3) match { case C(vs @ _*) => vs.foreach(f(_)) }
Wildcard imports
import java.util._
Hiding imports
import java.util.{ArrayList => _, _}
Joining letters to operators
def bang_!(x: Int) = 5
Assignment operators
def foo_=(x: Int) { ... }
Placeholder syntax
List(1, 2, 3) map (_ + 2)
Method values
List(1, 2, 3) foreach println _
Converting call-by-name parameters to functions
def toFunction(callByName: => Int): () => Int = callByName _
Default initializer
var x: String = _ // unloved syntax may be eliminated
Other
Match Expressions
def getClassAsString(x: Any): String = x match {
case s: String => s + " is a String"
case i: Int => "Int"
case f: Float => "Float"
case l: List[_] => "List"
case p: Person => "Person"
case _ => "Unknown"
}
def isTrue(a: Any) = a match {
case 0 | "" => false
case _ => true
}
// Version 1 - compiles to a tableswitch
import scala.annotation.switch
class SwitchDemo {
val i = 1
val x = (i: @switch) match {
case 1 => "One"
case 2 => "Two"
case _ => "Other"
}
}