Skip to content

Instantly share code, notes, and snippets.

View ghik's full-sized avatar

Roman Janusz ghik

View GitHub Profile
@ghik
ghik / PROFIT.scala
Created April 24, 2015 10:26
String interpolation + pattern matching = PROFIT
import RegexInterpolations._
val str = "My hovercraft is full of eels"
str match {
case r"""My (\w+)$vehicle is full of (\w+)$stuff""" =>
println(s"It is $stuff that my $vehicle is full of.")
}
@ghik
ghik / Opt.scala
Created April 20, 2015 15:10
Option as value class in Scala
import scala.language.implicitConversions
final class Opt[+A] private(private val value: Any) extends AnyVal {
def isEmpty = value == null
def isDefined: Boolean = !isEmpty
def get: A = value.asInstanceOf[A]
}
@ghik
ghik / stackable.scala
Created April 21, 2014 07:38
Stackable traits example
class Base {
def work() {
// do some work
}
}
trait Logging extends Base {
override def work() {
log("Doing work")
super.work()
@ghik
ghik / selftyped.scala
Last active August 29, 2015 13:56
Non-singleton self-types in Scala
// full implementation at https://github.com/ghik/selftyped
trait Base extends SelfTyped {
def same[T: Self]: T
def twice[T: Self]: T
}
trait SimpleSame extends Base {
// we can always safely return 'this' where self-type is expected