Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: Scala 2.10.0-M3
    • Fix Version/s: Scala 2.10.0
    • Component/s: Macros, Reflection
    • Labels:
      None

      Description

      In ScalaMock, I need to create instances of traits/classes that may contain abstract methods. To do this, I need to know the types of those methods. Right now, I'm using the following trick:

      scala> class Outer[T] { trait Inner[T]; def m(): Inner[T] = null }
      defined class Outer
      
      scala> val t = typeOf[Outer[Int]]
      t: reflect.runtime.universe.Type = Outer[Int]
      
      scala> val m = t.member(newTermName("m"))
      m: reflect.runtime.universe.Symbol = method m
      
      scala> m.typeSignatureIn(SuperType(ThisType(t.typeSymbol), t))
      res0: reflect.runtime.universe.Type = ()Outer.this.Inner[Int]
      

      described by Martin as:

      That looks like an ingenious exploitation of something that was not meant to be used that way. I do not see why it would not work, though. In general, I believe it's better to create an actual val with the proper type that can serve as prefix type.

      Unfortunately, this leaves me with a catch-22 - I can't create an instance of that trait without providing implementations of all of its methods. And I can't implement its methods without knowing their types.

      Apparently there is a narrow method that does what I need. It would be helpful if it could be added to the reflection API.

        Activity

        Hide
        Eugene Burmako added a comment -
        Show
        Eugene Burmako added a comment - Implemented in https://github.com/scala/scala/pull/751

          People

          • Assignee:
            Eugene Burmako
            Reporter:
            Paul Butcher
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development