Skip to content

Instantly share code, notes, and snippets.

@timperrett
Forked from viktorklang/some
Created July 6, 2009 12:37
Show Gist options
  • Save timperrett/141408 to your computer and use it in GitHub Desktop.
Save timperrett/141408 to your computer and use it in GitHub Desktop.
abstract class Foo[X](val value : X) { type T = X }
abstract class FooFactory[S <: Foo[_]](val P : (S#T) => boolean)
{
def apply(t : S#T) : Option[S] = if(P(t)) Some(fetch(t)) else None
def unapply(s : S) : Option[S#T] = if(s == null) None else Some(s.value)
def fetch(t : S#T) = mk(t)
protected[this] val mk : (S#T) => S
}
class Name private (v : String) extends Foo(v)
object Name extends FooFactory[Name](_.length > 0){ val mk = new Name(_)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment