在Scala中键入Lambda:为什么声明中需要额外的括号?

jhe*_*dus 12 grammar types scala

介绍:

根据我的理解,类型声明{type ?[?] = Either[A, ?]}代表任何具有其他类型?[?]作为其成员的类型(与方法是类的成员完全相同).这是一种结构类型,即它的结构是它具有类型别名声明?[?]作为其成员.

在另一方面,({type ?[?] = Either[A, ?]})#?是指 ?由于经由类型的投影#.

题:

为什么{type ?[?] = Either[A, ?]}在进行类型投影时需要括号?为什么不只是{typeλ[α] = [A,α]}#λ?

换句话说,什么是确切的解析树({type ?[?] = Either[A, ?]})#?根据Scala的类型声明语法(见下文)?

为什么{type ?[?] = Either[A, ?]}#?这个语法中没有正确的"句子"?

  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}
Run Code Online (Sandbox Code Playgroud)

Gab*_*lla 12

你还需要考虑

CompoundType    ::=  AnnotType {‘with’ AnnotType} [Refinement]
                  |  Refinement
Refinement      ::=  [nl] ‘{’ RefineStat {semi RefineStat} ‘}’
RefineStat      ::=  Dcl
                  |  ‘type’ TypeDef
                  |
Run Code Online (Sandbox Code Playgroud)

非正式描述

#只能跟随一个SimpleType,但是{type ?[?] = Either[A, ?]}一个Refinement,最终是一个Type.

SimpleType从泛型获得a的唯一方法Type是用括号括起来.

形式推导

SimpleType
'(' Types ')' '#' id
'(' Type ')' # id
'(' InfixType ')' # id
'(' CompoundType ')' # id
'(' Refinement ')' # id
'(' '{' RefineStat '}' ')' # id
'(' '{' 'type' TypeDef '}' ')' # id
         ...
({ type ?[?] = Either[A, ?] })#?
Run Code Online (Sandbox Code Playgroud)


rig*_*old 8

#要求SimpleType:

SimpleType ‘#’ id

但是,{ … }是一个Refinement(见CompoundType),SimpleType除非括号括起来(见‘(’ Types ‘)’).

所以,解析树({type ?[?] = Either[A, ?]})#?如下:

      SimpleType ‘#’ id
          /           \
   ‘(’ Types ‘)’      '?'
         |
        Type
         =
      InfixType
         =
    CompoundType
         =
     Refinement
Run Code Online (Sandbox Code Playgroud)