9

Is it possible to defer type member constraints to the usage site?

trait Query {
  type Result
}

Pseudocode:

def fooRequiresAnyRefResults[Q <: Query, Q#Result <: AnyRef]()
Lawrence Wagerfield
  • 6,471
  • 5
  • 42
  • 84

1 Answers1

8

I can't test it right now, but this should work :

def f[Q <: Query {type Result <: AnyRef}]() = ...

Marth
  • 23,920
  • 3
  • 60
  • 72
  • 2
    @LawrenceWagerfield How about accepting the answer? :) – ghik Mar 03 '14 at 00:09
  • Sorry, thought I had! – Lawrence Wagerfield Mar 03 '14 at 09:33
  • I take it that this means there's no way (in Scala 2.12), to declare the "<: AnyRef" constraint right at the type declaration, i.e. "trait Query { type Result <: AnyRef }" – akauppi Jul 25 '18 at 11:53
  • @akauppi: I'm not sure what you mean, your code should compile just fine in scala 2.12 – Marth Jul 25 '18 at 13:09
  • 1
    You are right. My case is ": Decoder" instead of the "<: AnyRef", and that one does not compile. This, however, would likely need a new SO question to be created. – akauppi Jul 26 '18 at 08:44
  • 1
    It was explained here: https://stackoverflow.com/questions/20440279/using-a-context-bound-in-a-class-type-parameter#20440368 – akauppi Jul 26 '18 at 08:58