开发者

Accessing type members outside the class in Scala

开发者 https://www.devze.com 2022-12-23 16:25 出处:网络
I am trying to understand type members in Scala. I wrote a simple example that tries to explain my question.

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 = ty开发者_JAVA百科peTest.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号