Last active
June 12, 2016 17:20
-
-
Save ktoso/4645350 to your computer and use it in GitHub Desktop.
An proof of concept implementation to Adam's "what if we just used the types" blog post http://www.warski.org/blog/2013/01/dry-parameter-names/
This file contains hidden or 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
ktoso@moon /Users/ktoso | |
$ scalac vals_by_types.scala | |
warning: there were 2 deprecation warnings; re-run with -deprecation for details | |
one warning found | |
ktoso@moon /Users/ktoso | |
$ scala Main | |
The combined names are: UserFinder and UserStatusReader |
This file contains hidden or 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 HasClassName { def name = getClass.getSimpleName } | |
class UserFinder extends HasClassName | |
class UserStatusReader extends HasClassName | |
class Thing(implicit val a: UserFinder, | |
implicit val b: UserStatusReader) { | |
def combineNames() = { | |
implicitly[UserFinder].name + implicitly[UserStatusReader].name | |
} | |
} | |
object Main extends App { | |
val thing = new Thing()(new UserFinder, new UserStatusReader) | |
println(s"The combined names are: ${thing.combineNames()}") | |
} |
You could also hide implicitly[T] behind to closes mimic the syntax the article proposed:
def the [T](implicit ev: T) = implicitly[T]
So you can get following:
def combineNames() = {
the[UserFinder].name + the[UserStatusReader].name
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just to be clear - I wouldn't advocate such code ;-) It feels really off to me; but well, a POC is a POC - you could do it it you want to