Scala syntax/grammar 类型系统的例子?
Scala syntax/grammar of type system example?
我正在学习 scala。
在 scala 语法中:https://www.scala-lang.org/files/archive/spec/2.13/13-syntax-summary.html,我读到了 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}
| ‘:’ ‘_’ ‘*’
我可以举一些例子:
- 当
StableId
为 Int
且 StableId => SimpleType => AnnotType => CompoundType => InfixType => Type
时,我们将得到一个名为 Int
的 Type
。
- 当
FunctionArgTypes
为()
,Type
为Int
时,我们将得到一个名为()=>Int
的Type
。
- 当
Type
是Array
,TypeArgs
是[Int]
,我们会得到SimpleType
= Array[Int]
,那么Type
是 Array[Int]
.
我不明白某些类型系统:
InfixType
是什么?
CompoundType
是什么?
Refinement
是什么?
Ascription
是什么?
- 什么时候
SimpleType
会导出一个SimpleType '#' id
,什么时候会导出'(' Types ')'
?
有没有 scala 类型系统的例子?
例如Int Op String
中的Op
是中缀类型
trait Op[A, B]
type T = Int Op String
例如Int with String with Boolean
是复合类型
例如{ type X }
是
中的类型细化
trait MyTrait
type T = MyTrait { type X }
例如1 : Int
中的: Int
是类型归属。
例如MyTrait#T
是类型投影
trait MyTrait {
type T
}
类型
type T = (Int, String, Boolean)
是元组类型。
我正在学习 scala。
在 scala 语法中:https://www.scala-lang.org/files/archive/spec/2.13/13-syntax-summary.html,我读到了 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}
| ‘:’ ‘_’ ‘*’
我可以举一些例子:
- 当
StableId
为Int
且StableId => SimpleType => AnnotType => CompoundType => InfixType => Type
时,我们将得到一个名为Int
的Type
。 - 当
FunctionArgTypes
为()
,Type
为Int
时,我们将得到一个名为()=>Int
的Type
。 - 当
Type
是Array
,TypeArgs
是[Int]
,我们会得到SimpleType
=Array[Int]
,那么Type
是Array[Int]
.
我不明白某些类型系统:
InfixType
是什么?CompoundType
是什么?Refinement
是什么?Ascription
是什么?- 什么时候
SimpleType
会导出一个SimpleType '#' id
,什么时候会导出'(' Types ')'
?
有没有 scala 类型系统的例子?
例如
Int Op String
中的Op
是中缀类型trait Op[A, B] type T = Int Op String
例如
Int with String with Boolean
是复合类型例如
中的类型细化{ type X }
是trait MyTrait type T = MyTrait { type X }
例如
1 : Int
中的: Int
是类型归属。例如
MyTrait#T
是类型投影trait MyTrait { type T }
类型
type T = (Int, String, Boolean)
是元组类型。