Created
June 14, 2017 09:59
-
-
Save ukitaka/c43982224880794598e5d86fe4fda64f to your computer and use it in GitHub Desktop.
FromTrait - https://rust-lang-ja.github.io/the-rust-programming-language-ja/1.6/book/error-handling.html
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
trait From[T, S] { | |
def from(t: T): S | |
} | |
object From { | |
def from[T, S](t: T)(implicit F: From[T, S]): S = F.from(t) | |
} | |
implicit val fromIntToString = new From[Int, String] { | |
def from(i: Int): String = i.toString | |
} | |
implicit val fromDoubleToString = new From[Double, String] { | |
def from(d: Double): String = d.toString | |
} | |
implicit val fromBooleanToString = new From[Boolean, String] { | |
def from(b: Boolean): String = b.toString | |
} | |
val str1: String = From.from(1) | |
val str2: String = From.from(true) | |
val str3: String = From.from(4.2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment