为什么榆树对空白如此挑剔,但仅限于某些情况?

时间:2016-06-19 02:13:04

标签: elm

为什么此代码失败

type SectionedItems s i = SectionedItems{
  section : s,
  items : List i,
  subsections: List (SectionedItems s i)
}

si1 : SectionedItems String String
si1 = SectionedItems{
  section  =  "",
  items = [
    "1",
    "2"
  ],
  subsections = [

  ]
}

并且此代码成功

type SectionedItems s i = SectionedItems{
  section : s,
  items : List i,
  subsections: List (SectionedItems s i)
}

si1 : SectionedItems String String
si1 = SectionedItems{
  section  =  "",
  items = [
    "1",
    "2"
  ],
  subsections = [

  ]
                    }

为什么elm第一个代码失败了?我知道它因空白而失败,但为什么呢?为什么在创建实例时必须对齐{},而在声明类型时却不一致?

1 个答案:

答案 0 :(得分:1)

并不是那些括号必须排成一行,而是你不能把右括号放在行的开头。

例如,这会编译:

si1 : SectionedItems String String
si1 = SectionedItems{
  section  =  "",
  items = [
    "1",
    "2"
  ],
  subsections = [

  ]
 }

在结束括号之前加上一个额外的空格就足够了。

为什么呢?因为si1的“孩子”必须比si1本身有更大的缩进。如果他们不这样做,Elm认为您正在尝试开始新的定义,}不是开始定义的有效方法。