Scala和typecheck为依赖类型

时间:2012-11-20 12:57:23

标签: scala testing typechecking

给定Java类“抽象凭证”并派生“EmailPassword”/“OAuth”类。

和界面CredentialsStorage,具有适当的实现EmailPasswordStorage和OAuthStorage

我需要创建一些带三元组的数组:存储,有效凭据,无效凭据)

所以我从这开始:

type T <: Credentials

type S <: CredentialsStorage[T]

private var testData: Array[(S, T, T)] = Array(
  (emailStorage, validEmailPasswd, new EmailPasswordCredentials("1", "2")),
  (oAuthStorage, validAuthToken, new OAuthCredentials("invalid auth token", OAuthService.FACEBOOK))
)

然而这不会编译

error: type mismatch;
found   : storage.EmailPasswordStorage[credentials.EmailPassword]
required: CredentialsStorageTest.this.S
(emailStorage, validEmailPasswd, new EmailPasswordCredentials("1", "2")),

如何解决此问题以及依赖/存在类型的正确定义是什么?

UPD 我解决了元组类型本身定义的问题:

type T[A] = (CredentialsStorage[A],A,A)

val testData : Array[T[_ <: Credentials]] = ...

1 个答案:

答案 0 :(得分:2)

type T <: Credentials定义了一种抽象类型。您尚未定义此实例中的T。请尝试添加type T = credentials.EmailPasswordtype S = storage.EmailPasswordStorage[T]