문제

I am trying to understand type members in Scala. I wrote a simple example that tries to explain my question.

First, I created two classes for types:

class BaseclassForTypes
class OwnType extends BaseclassForTypes

Then, I defined an abstract type member in trait and then defined the type member in a concerete class:

trait ScalaTypesTest {
  type T <: BaseclassForTypes

  def returnType: T
}

class ScalaTypesTestImpl extends ScalaTypesTest {
  type T = OwnType

  override def returnType: T = {
    new T
  }
} 

Then, I want to access the type member (yes, the type is not needed here, but this explains my question). Both examples work.

Solution 1. Declaring the type, but the problem here is that it does not use the type member and the type information is duplicated (caller and callee).

val typeTest = new ScalaTypesTestImpl
val typeObject:OwnType = typeTest.returnType // declare the type second time here
true must beTrue

Solution 2. Initializing the class and using the type through the object. I don't like this, since the class needs to be initialized

val typeTest = new ScalaTypesTestImpl
val typeObject:typeTest.T = typeTest.returnType // through an instance
true must beTrue

So, is there a better way of doing this or are type members meant to be used only with the internal implementation of a class?

도움이 되었습니까?

해결책

You can use ScalaTypesTestImpl#T instead of typeTest.T, or

val typeTest:ScalaTypesTest = new ScalaTypesTestImpl
val typeObject:ScalaTypesTest#T = typeTest.returnType

다른 팁

If you don't want to instance ScalaTypesTestImpl, then, perhaps, you'd be better off putting T on an object instead of class. For each instance x of ScalaTypesTestImpl, x.T is a different type. Or, in other words, if you have two instances x and y, then x.T is not the same type as y.T.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top