Created
July 31, 2010 20:26
-
-
Save retronym/502572 to your computer and use it in GitHub Desktop.
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
abstract class LogFormatter[-A] { | |
def format(a: A): String | |
} | |
object LogFormatter { | |
implicit object StringLogFormatter extends LogFormatter[String] { | |
def format(s: String) = s | |
} | |
implicit object ExceptionLogFormatter extends LogFormatter[Exception] { | |
def format(e: Exception) = e.getMessage | |
} | |
} | |
object Logger { | |
def log(level: Int) = new { | |
def apply[A: LogFormatter](a: A) { | |
val formatted = implicitly[LogFormatter[A]].format(a) | |
} | |
} | |
} | |
val x = Logger.log(0) | |
x("s") | |
x(new RuntimeException("bang!")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment