Skip to content

Instantly share code, notes, and snippets.

@f81
Created March 31, 2014 11:24
Show Gist options
  • Save f81/9890226 to your computer and use it in GitHub Desktop.
Save f81/9890226 to your computer and use it in GitHub Desktop.
第22章:Scalaの抽出子 ref: http://qiita.com/f81@github/items/109dbe953dac516bd71a
$ scalac User.scala
$ scala scala User
赤石 剛次
scala> class ApplyClass(val message: String); object ApplyClass { def apply(message: String): ApplyClass = { new ApplyClass(message) } }
defined class ApplyClass
defined module ApplyClass
scala> ApplyClass("乱坊少佐")
res0: ApplyClass = ApplyClass@43679ffd
def apply[A](elems: A*): Seq[A]
class User(private val firstname: String, private val lastname: String)
object User {
def unapply(user: User) = Option((user.firstname, user.lastname))
}
def main(args: Array[String]): Unit = {
val user = new User("剛次", "赤石")
user match {
case User(firstname, lastname) => println(lastname + " " + firstname)
case _ => println("Not user.")
}
}
object User1 {
def main(args: Array[String]): Unit = {
val user = new User("剛次", "赤石")
val User(firstname, lastname) = user
println(lastname + " " + firstname)
}
}
val seq = Seq(1, 2, 3, 4)
val Seq(x, y , _*) = seq
scala> object ApplyTest { def apply(message: String) { println(message) } }
scala> ApplyTest.apply("乱坊少佐")
乱坊少佐
scala> ApplyTest("乱坊少佐")
乱坊少佐
import scala.Option
class User(private val firstname: String, private val lastname: String)
object User {
def unapply(user: User) = Option((user.firstname, user.lastname))
def main(args: Array[String]): Unit = {
val user = new User("剛次", "赤石")
user match {
case User(firstname, lastname) => println(lastname + " " + firstname)
case _ => println("Not user.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment