Skip to content

Instantly share code, notes, and snippets.

@ktoso
Last active June 12, 2016 17:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktoso/4645350 to your computer and use it in GitHub Desktop.
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/
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
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()}")
}
@ktoso
Copy link
Author

ktoso commented Jan 27, 2013

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

@przemek-pokrywka
Copy link

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