Dan Li
Dan Li

Reputation: 928

Specifying a method parameter's type as singleton object's type

I'd like to write a method that takes a specific singleton object type, like this:

object X
def foo(x: X.type) = ???    // this doesn't compile

However, this doesn't compile.

If you're wondering, my actual use case is the following:

class Outer { object Inner }
def foo(x: Outer#Inner.type) = ???    // this doesn't compile

Is this possible?

Upvotes: 1

Views: 46

Answers (1)

pedrofurla
pedrofurla

Reputation: 12783

The first case does compile.

scala> object X
defined object X

scala> def foo(x: X.type) = ??? 
foo: (x: X.type)Nothing

The second case, I think the problem is a bit of syntactical deficiency. One work around it could be something like:

scala> class Outer { object Inner; type InnerType = Inner.type  }
defined class Outer

scala> def foo(x: Outer#InnerType) = ??? 
foo: (x: _1.Inner.type forSome { val _1: Outer })Nothing

Upvotes: 3

Related Questions