Skip to content

Instantly share code, notes, and snippets.

@ktoso
Created January 26, 2015 09:28
Show Gist options
  • Select an option

  • Save ktoso/b36cb880ef16683eed29 to your computer and use it in GitHub Desktop.

Select an option

Save ktoso/b36cb880ef16683eed29 to your computer and use it in GitHub Desktop.
trait Box {
type T
private var t: T = _
def put(t: T) = this.t = t
def get = t
}
{ // traits only
trait IntBox extends Box { override type T = Int }
val intBox = new IntBox {}
intBox.put(1)
val t = intBox.get // inferred: Int
def extract(box: Box): box.T = box.get
val got = extract(intBox) // inferred: Int
}
{ // class IntBox
class IntBox extends Box { override type T = Int }
val intBox = new IntBox {}
intBox.put(1)
val t = intBox.get // inferred: intBox.T
def extract(box: Box): box.T = box.get
val got = extract(intBox) // inferred: intBox.T
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment