- Learn Scala Programming
- Slava Schmidt
- 416字
- 2021-06-10 19:35:44
Ways to create a type
There are four ways to define a type in Scala:
- By using a literal to define a singleton type
- By using the type keyword to define an alias for an abstract or concrete type
- By defining a class, object, or trait to create a concrete type
- By defining a method that creates a method type
Singleton types were introduced in Scala 2.13, and we'll look at them in detail later in this chapter. For now, let's try to define and refer to some concrete types in Scala REPL:
scala> class SomeClass
defined class SomeClass
scala> object SomeObject
defined object SomeObject
scala> :type SomeObject
SomeObject.type
scala> :type new SomeClass
SomeClass
scala> trait SomeTrait {
| def usage(a: SomeClass, b: SomeObject.type): SomeTrait
| }
defined trait SomeTrait
The type can be referred before it is fully defined, as shown by the example of SomeTrait.
When annotating types, traits and classes can be used directly, but the type of an object needs to be referenced by using its type operator. The a.type form in Scala describes a singleton type. Depending upon whether p conforms to scala.AnyRef, it denotes either a set of values, [a, null], or just an a. As we usually don't use null in Scala programs, we can say that a.type denotes a type containing a single element, a. Normally, it is not used in regular code because it is easier to reference an object directly than to pass it as a parameter, but this style has found its use in some advanced libraries to implement parts of the internal DSL.
In Scala 2.13, there is a new marker trait Singleton that can be applied as an upper bound on a type parameter, which indicates that the singleton type should be inferred for this parameter:
scala> def singleIn[T <: Singleton](t: T): T = t
singleIn: [T <: Singleton](t: T)T
scala> final val t = singleIn(42)
t: 42 = 42
A method type does not denote a value, nor does it appear directly in the program. It is an internal representation of a method definition. It is represented as a sequence of parameter names with respective types and a return type of the method. The method type is important into know about because, if a method name is used as a value, its type is implicitly converted to the corresponding function type. As we defined in the usage method, the compiler internally created a method type called (a: SomeClass, b: SomeObject.type)SomeTrait.
- JavaScript百煉成仙
- Java 開發(fā)從入門到精通(第2版)
- Oracle從新手到高手
- Mastering Articulate Storyline
- Access 2010數(shù)據(jù)庫應(yīng)用技術(shù)(第2版)
- C語言程序設(shè)計(jì)
- Java網(wǎng)絡(luò)編程實(shí)戰(zhàn)
- 實(shí)戰(zhàn)Java高并發(fā)程序設(shè)計(jì)(第2版)
- Python趣味編程與精彩實(shí)例
- MyBatis 3源碼深度解析
- Sails.js Essentials
- C語言程序設(shè)計(jì)實(shí)踐
- C++面向?qū)ο蟪绦蛟O(shè)計(jì)
- 分布式系統(tǒng)架構(gòu)與開發(fā):技術(shù)原理與面試題解析
- Visual C++程序開發(fā)范例寶典