简体   繁体   English

继承中的默认类型参数

[英]Default type parameter in inheritence

Say I have the following classes: 说我有以下课程:

class A[T] { ... }

abstract class B[T1,T2](t: T1)(implicit ev: A[T2]) {
... 
}

On some instances, when I inherit from B, the type for T2 is the same as that for T1. 在某些情况下,当我从B继承时,T2的类型与T1的类型相同。 Is there I way I can define my class to avoid having to specify this explicitly? 有没有我可以定义我的类以避免必须明确指定它?

So rather than having to do: 所以,而不是必须这样做:

class C extends B[String, String]("Some string") {
...
}

Can I have the compiler some how infer this so I only need to write: 我可以让编译器有一些推断,所以我只需要写:

class C extends B("Some string") {
...
}

I think a type alias should work: 我认为类型别名应该有效:

type B1[T] = B[T, T]

class C extends B1("Some string") {
   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM