michaelmesser
michaelmesser

Reputation: 3726

Why is elm so picky about whitespace but only in certain cases?

Why does this code fail

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 = [

  ]
}

and this code succeeds

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 = [

  ]
                    }

Why does elm fail for the first code? I know it is failing due to whitespace but why? Why do the { and } have to be aligned when creating an instance but not when declaring the type?

Upvotes: 1

Views: 209

Answers (1)

Kris Jenkins
Kris Jenkins

Reputation: 4210

It's not that those brackets have to be lined up, it's that you can't put the closing bracket at the start of the line.

For example, this compiles:

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

  ]
 }

Just putting in one extra space before the closing bracket is enough.

Why? Because the "children" of si1 must have greater indentation than si1 itself. If they don't, Elm thinks you're trying to start a new definition, and } isn't a valid way to start a definition.

Upvotes: 1

Related Questions