Scala(和Java)中的类和类型之间有什么区别? [英] What is the difference between a class and a type in Scala (and Java)?

查看:128
本文介绍了Scala(和Java)中的类和类型之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala



在Scala中,类和类型之间的差异区分重要吗?



这只是语言设计观点的考虑,还是在编程Scala时会产生实际影响?



或者是类型系统的保证边界的基础( Nothing Null



在Java中还可以识别上面提到的多少注意事项/差异/问题?






a href =http://stackoverflow.com/questions/468145/what-is-the-difference-between-type-and-class>类型和类之间有什么区别?作为一种语言 - 不确定的介绍。)

解决方案

当你说type时,我想假设你的意思是静态类型。



静态类型是程序的一部分的属性,可以静态证明(静态意味着不运行 )。在静态类型语言中,每个表达式都有一个类型,不管你是否写它。例如,在Cishint x = a * b + c-d中,a,b,c和d具有类型,a * b具有类型,a * b + c具有类型, c -d有一个类型。但是我们只用一个类型注释了x。在其他语言,如Scala,C#,Haskell,SML和F#,即使这是没有必要的。



究竟什么属性可证明取决于类型检查器。



另一方面,Scala样式类只是一组对象的规范。该规范包括一些类型信息,并且包括大量的实现和表示细节,例如方法体和私有字段等。在Scala中,类还指定了一些模块边界。



许多语言都有类型,但没有类,并且许多语言都有类,但没有类(类)。



类和类之间有几个可观察的差异。 List [String]是一个类型,但不是一个类。在Scala List是类,但通常不是一个类型(它实际上是一个更高类型的类型)。在C#列表不是任何类型的类型,在Java它是一个原始类型。



Scala提供结构类型。 {def foo:Bar}表示任何可以具有返回Bar的foo方法的对象,而不管类。它是一个类型,但不是类。



类型可以使用类型参数抽象。当你写def foo [T](x:T)= ...,那么在foo T的内部是一个类型。但是T不是一个类。



类型在Scala中可以是虚拟的(即抽象类型成员),但是类不能在今天使用Scala一种用于对虚拟类进行编码的粗糙方式 http://lampsvn.epfl.ch/trac/scala/wiki/VirtualClasses



现在,动态类型。动态类型是运行时在执行某些操作之前自动检查的对象的属性。在动态类型的基于类的OO语言中,类型和类之间存在很强的相关性。同样的事情发生在JVM语言,如Scala和Java,其操作只能动态检查,如反射和铸造。在这些语言中,类型擦除或多或少意味着大多数对象的动态类型与它们的类相同。或多或少。这不是真的,例如,通常不被擦除的数组,以便运行时可以告诉Array [Int]和Array [String]之间的差异。但请记住我的广义定义动态类型是运行时自动检查的对象的属性。当您使用反射时,可以向任何对象发送任何消息。如果对象支持该消息,那么一切正常。因此,谈论所有可以像鸭子一样作为动态类型的对象是有意义的,即使它不是一个类。这就是Python和Ruby社区称为鸭式打字的本质。此外,通过我的广义定义,即使zeroness是一种动态类型,在大多数语言中,运行时自动检查数字,以确保不除以零。有非常少的语言可以通过使静态类型为零(或不为零)来静态地证明。



最后,正如其他人提到的,类型如int,没有一个类作为一个实现细节,类型像Null和任何有点特别,但COULD有类和没有,类型像Nothing,甚至没有任何值更不用说一个类。


Scala

Where can differences between a class and a type be observed in Scala and why is this distinction important?

Is it only a consideration from the language design point-of-view or has it "practical" impact when programming Scala?

Or is it fundamental to "securing the boundaries" of the type system (Nothing, Null come to my mind)?

Java

How many of the considerations/differences/problems mentioned above can also be recognized in Java?


(See What is the difference between Type and Class? as a language-agnostic introduction.)

解决方案

When you say "type" I'm going to assume you mean static type mostly. But I'll talk about dynamic types shortly.

A static type is a property of a portion of a program that can be statically proven (static means "without running it"). In a statically typed language, every expression has a type whether you write it or not. For instance, in the Cish "int x = a * b + c - d", a,b,c,and d have types, a * b has a type, a * b + c has a type and a * b + c -d has a type. But we've only annotated x with a type. In other languages, such as Scala, C#, Haskell, SML, and F#, even that wouldn't be necessary.

Exactly what properties are provable depends on the type checker.

A Scala style class, on the other hand, is just the specification for a set of objects. That specification includes some type information and includes a lot of implementation and representation details such as method bodies and private fields, etc. In Scala a class also specifies some module boundaries.

Many languages have types but don't have classes and many languages have classes but don't have (static) types.

There are several observable differences between types and classes. List[String] is a type but not a class. In Scala List is class but normally not a type (it's actually a higher kinded type). In C# List isn't a type of any sort and in Java it's a "raw type".

Scala offers structural types. {def foo : Bar} means any object that provably has a foo method that returns a Bar, regardless of class. It's a type, but not a class.

Types can be abstracted using type parameters. When you write def foo[T](x : T) = ..., then inside the body of foo T is a type. But T is not a class.

Types can be virtual in Scala (i.e. "abstract type members"), but classes can't be virtual with Scala today (although there's a boilerplate heavy way to encode virtual classes http://lampsvn.epfl.ch/trac/scala/wiki/VirtualClasses)

Now, dynamic types. Dynamic types are properties of objects that the runtime automatically checks before performing certain operations. In dynamically typed class-based OO languages there's a strong correlation between types and classes. The same thing happens on JVM languages such as Scala and Java which have operations that can only be checked dynamically such as reflection and casting. In those languages, "type erasure" more or less means that the dynamic type of most objects is the same as their class. More or less. That's not true of, e.g., arrays which aren't typically erased so that the runtime can tell the difference between Array[Int] and Array[String]. But remember my broad definition "dynamic types are properties of objects that the runtime automatically checks." When you use reflection it is possible to send any message to any object. If the object supports that message then everything works out. Thus it makes sense to talk of all objects that can quack like a duck as a dynamic type, even though it's not a class. That's the essence of what the Python and Ruby communities call "duck typing." Also, by my broad definition even "zeroness" is a dynamic type in the sense that, in most languages, the runtime automatically checks numbers to make sure you don't divide by zero. There are a very, very few languages that can prove that statically by making zero (or not-zero) a static type.

Finally, as other's have mentioned, there are types like int which don't have a class as an implementation detail, types like Null and Any which are a bit special but COULD have classes and don't, and types like Nothing which doesn't even have any values let alone a class.

这篇关于Scala(和Java)中的类和类型之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆