Created
March 31, 2014 11:24
-
-
Save f81/9890226 to your computer and use it in GitHub Desktop.
第22章:Scalaの抽出子 ref: http://qiita.com/f81@github/items/109dbe953dac516bd71a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scalac User.scala | |
$ scala scala User | |
赤石 剛次 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def apply[A](elems: A*): Seq[A] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Seq(1, 2, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User(private val firstname: String, private val lastname: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object User { | |
def unapply(user: User) = Option((user.firstname, user.lastname)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(args: Array[String]): Unit = { | |
val user = new User("剛次", "赤石") | |
user match { | |
case User(firstname, lastname) => println(lastname + " " + firstname) | |
case _ => println("Not user.") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object User1 { | |
def main(args: Array[String]): Unit = { | |
val user = new User("剛次", "赤石") | |
val User(firstname, lastname) = user | |
println(lastname + " " + firstname) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val seq = Seq(1, 2, 3, 4) | |
val Seq(x, y , _*) = seq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> object ApplyTest { def apply(message: String) { println(message) } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> ApplyTest.apply("乱坊少佐") | |
乱坊少佐 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> ApplyTest("乱坊少佐") | |
乱坊少佐 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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