I'm learning scala.
In scala syntax: https://www.scala-lang.org/files/archive/spec/2.13/13-syntax-summary.html, I read about the type system
.
Type ::= FunctionArgTypes ‘=>’ Type
| InfixType [ExistentialClause]
FunctionArgTypes ::= InfixType
| ‘(’ [ ParamType {‘,’ ParamType } ] ‘)’
ExistentialClause ::= ‘forSome’ ‘{’ ExistentialDcl {semi ExistentialDcl} ‘}’
ExistentialDcl ::= ‘type’ TypeDcl
| ‘val’ ValDcl
InfixType ::= CompoundType {id [nl] CompoundType}
CompoundType ::= AnnotType {‘with’ AnnotType} [Refinement]
| Refinement
AnnotType ::= SimpleType {Annotation}
SimpleType ::= SimpleType TypeArgs
| SimpleType ‘#’ id
| StableId
| Path ‘.’ ‘type’
| ‘(’ Types ‘)’
TypeArgs ::= ‘[’ Types ‘]’
Types ::= Type {‘,’ Type}
Refinement ::= [nl] ‘{’ RefineStat {semi RefineStat} ‘}’
RefineStat ::= Dcl
| ‘type’ TypeDef
|
TypePat ::= Type
Ascription ::= ‘:’ InfixType
| ‘:’ Annotation {Annotation}
| ‘:’ ‘_’ ‘*’
I can give some examples:
StableId
is Int
, and StableId => SimpleType => AnnotType => CompoundType => InfixType => Type
, we will get a Type
called Int
.FunctionArgTypes
is ()
, Type
is Int
, we will get a Type
called ()=>Int
.Type
is Array
, TypeArgs
is [Int]
, we will get SimpleType
= Array[Int]
, then Type
is Array[Int]
.I don't understand some of the type system:
InfixType
?CompoundType
?Refinement
?Ascription
?SimpleType
derive a SimpleType '#' id
, and when will derive '(' Types ')'
?Is there any examples for scala type system ?
For example Op
in Int Op String
is an infix type
trait Op[A, B]
type T = Int Op String
For example Int with String with Boolean
is a compound type.
For example { type X }
is a type refinement in
trait MyTrait
type T = MyTrait { type X }
For example : Int
in 1 : Int
is a type ascription.
For example MyTrait#T
is a type projection
trait MyTrait {
type T
}
Type
type T = (Int, String, Boolean)
is a tuple type.