Scala类型参数在各地 [英] Scala type parameters in various places
问题描述
我刚刚开始学习scala,但对类型参数的一般理解有些问题.以前我学过Haskell,但是在scala中,这似乎让我感到困惑.对于我来说,我不清楚应该在哪里放置类型参数(在类定义或函数定义附近).
I have just started learning scala and have some issues with general understanding of type parameters. Previously I learned Haskell, but in scala it seems very confusing to me. It is unclear for me where should I put type parameters (near class definition or function definition).
考虑示例:
class Person[A] {
def sayName (name: A) = println(name)
}
将类型参数从类移动到函数是否有意义?
Does it make sense to move type parameter from class to function?
class Person {
def sayName[A] (name: A) = println(name)
}
或者我什至可以在类和函数中都保留类型参数,它将起作用.有什么大不同吗? 函数中的[A]参数会覆盖类定义中的相同参数吗?
Or I even can leave type parameter both in class and function and it will work. Does it make some big difference? Will the [A] parameters in function override same param from class definition?
我可以创建Person的实例或调用函数而无需指出确切的类型.
And I can create the instance of Person or call a function without pointing the exact type.
val p = new Person();
那为什么呢?只是在我需要通用的情况下? 因此,对于我来说,尚不清楚何时应将类型参数放在什么位置(类或函数)上.
So why is this for? Just in cases when I want smth generic? So it is unclear for me when and on which positions (class or function) should I put type parameters.
推荐答案
-
如果您在 class 级别声明type参数,则会在 construction 上分配实际的类型,并且以后不能更改-所有的调用给定实例上的
sayName
必须使用相同的类型If you declare the type parameter at the class level, you assign the actual type upon construction, and can't change it later - all invocations of
sayName
on a given instance will have to use the same type如果在方法级别上取消类型参数,则可以在方法的每个调用中分配不同的类型
If you desclate the type parameter at the method level, you can assign a different type in each invocation of the method
因此-如果类的实例应始终应用于单个类型,请使用类级别的定义.
So - if an instance of the class should always apply to a single type - use class-level definition.
例如:
trait Animal case class Dog() extends Animal case class Cat() extends Animal // A single owner has a *specific* pet, // so it makes sense to declare type at class level class Owner[A <: Animal] { def feed(a: A) = ??? } // A single RandomAnimalLover walking down the street, // might feed both Dogs and Cats - so their feed method must be parameterized, and there's no use in adding a parameter at the class level class RandomAnimalLover { def feed[A <: Animal](a: A) = ??? } val dog = Dog() val cat = Cat() val ownerA = new Owner[Dog] val randomDude = new RandomAnimalLover ownerA.feed(dog) // compiles ownerA.feed(cat) // does not compile randomDude.feed(dog) // compiles randomDude.feed(cat) // compiles
最后,如果您在类级别和方法级别同时声明了一个类型参数两者,则这是两个单独的参数.如果它们的名称不同-两者都可以在任何地方使用;如果它们具有相同的名称,则该方法级别的参数将阴影在该方法的定义和实现的范围内(但在该类的其他地方).
Lastly, if you declare a type parameter both at the class level and at the method level - these are two separate parameters. If they have different names - both can be used anywhere; If they have the same name, the method-level parameter will shadow the class level parameter within the scope of that method's definition and implementation (but nowhere else in the class).
这篇关于Scala类型参数在各地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!