简体   繁体   English

Scala:是否可以将类型参数约束为非抽象?

[英]Scala: Is it possible to constrain a type parameter to be non abstract?

Scala: Is it possible to constrain a type parameter to be non abstract? Scala:是否可以将类型参数约束为非抽象?

Are any other constraints possible except view bounds, upper bounds and lower bounds for type parameters and abstract type members? 除了视图边界,类型参数的上限和下限以及抽象类型成员之外,是否还有其他约束? In C# for example, which I'm familiar with, you have the additional generic constraints: 例如,在我熟悉的C#中,您有其他通用约束:

where T: Class //Not sure if this covered in Scala by T<: AnyRef 其中T:Class //不确定Scala中是否包含T <:AnyRef

where T: interface 其中T:接口

where T: struct 其中T:struct

where U: T //naked type constraint 其中U:T //裸体约束

where T: new () //This ensures that a type parameter is non-abstract and allows one to instantiate an object of the type in the generic class. 其中T:new()//这可以确保类型参数是非抽象的,并允许实例化泛型类中的类型对象。

The last one is particularly important as it allows you to construct your unknown type, although its a shame you can only proscribe a parameterless constructor. 最后一个特别重要,因为它允许你构造你的未知类型,虽然很遗憾你只能禁止无参数构造函数。

Can =:= <:< and <%< only be used on method parameters? Can =:= <:<和<%<仅用于方法参数?

In response to the comments, the immediate trigger for the question was the need for a "T: new()" restraint or some equivalent mechanism. 在回应评论时,问题的直接触发因素是需要“T:new()”克制或某种等效机制。

class ExampleClass[T <: AnyRef] {
  val example: T = new T()//Won't compile as the compiler  
} //doesn't know if such a constructor exists

The uses of some of the C# constraints are particular to the needs of C#. 一些C#约束的使用特别适合C#的需求。 For example one restraint that you don't have in C# that people are always wanting is "T: numericType" That issue is already solved in Scala. 例如,人们一直想要的C#中没有的一个约束是“T:numericType”这个问题已经在Scala中解决了。 I'm still very much boot strapping my way up the Scala language, so aside from the above, I was just trying to clarify exactly what tools are and are not available to me in this facet of Scala syntax, even though I don't know yet exactly how I might want to use them in the Scala context. 我仍然非常喜欢使用Scala语言,所以除了上述内容之外,我只是想在Scala语法的这个方面确切地说明哪些工具是不可用的,即使我不这样做我知道如何在Scala上下文中使用它们。

I'm not sure if this is fully related but sometimes it seems the compiler (Eclipse 2.1.0.M1 with Eclipse 3.7.2) won't let me instantiate collections of unknown element type. 我不确定这是否完全相关,但有时似乎编译器(带有Eclipse 3.7.2的Eclipse 2.1.0.M1)将不允许我实例化未知元素类型的集合。 The following code now seems to compiles fine. 以下代码现在似乎编译好了。 So I'd like to know what the rules are: 所以我想知道规则是什么:

abstract class Descrip [T <: DTypes]()
{      
  val hexs: MutableList[T#HexT] = new  MutableList[T#HexT] //compiles fine
  val sides: MutableList[T#SideT] = new MutableList[T#SideT] //compiles fine
}

The need of new T() is normally resolved by using Manifest s. 通常使用Manifest来解决对new T()的需求。 For example this code compiles: 例如,此代码编译:

class ExampleClass[T: Manifest] {
  val example: T = manifest[T].erasure.newInstance().asInstanceOf[T]
}

The drawback is that if the T for which ExampleClass is instantiated doesn't have a no-arg constructor it will fail at runtime... 缺点是如果实例化ExampleClassT没有no-arg构造函数,它将在运行时失败...

Regarding Numeric , Ordering , etc., they are type classes and are part of the standard library, not part of the language. 关于NumericOrdering等,它们是类型类 ,是标准库的一部分,不是语言的一部分。 You can build your own type classes. 您可以构建自己的类型类。 The case of Manifest is special because it do have support from the language to provide the implicit object. Manifest的情况很特殊,因为它确实得到语言的支持来提供隐式对象。

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

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