elm / error-message-catalog

A catalog of broken Elm programs / data to improve error messages
BSD 3-Clause "New" or "Revised" License
174 stars 17 forks source link

Add recommendation for type vs. type alias #2

Open TheSeamau5 opened 9 years ago

TheSeamau5 commented 9 years ago

It's cool that the compiler recommends the use of types instead of type aliases when things get recursive, but I think that it would be cool if there was a recommendation that goes the other way.

For example:

type Vector = { x : Float, y : Float }

Yields:

-- SYNTAX PROBLEM ------------------------------------------------------------- 

I ran into something unexpected when parsing your code!

0| 
1| type Vector = { x : Float, y : Float }
2|               ^

I am looking for one of the following things:

   a constructor for a union type
   whitespace

Ideally, you'd like something to say:

-- SYNTAX PROBLEM ------------------------------------------------------------- 

I ran into something unexpected when parsing your code!

0| 
1| type Vector = { x : Float, y : Float }
2|               ^

Perhaps you have meant to create a type alias.

    type alias Vector = { x : Float, y : Float }

and then with an explanation of the difference between types and type aliases. This could be useful, especially for people coming from Haskell or beginners who still haven't grasped the difference between type and type alias

narkisr commented 9 years ago

As a noob myself the:

I am looking for one of the following things:

    a constructor for a union type
    whitespace

Means nothing, the docs on type aliases are lacking also http://elm-lang.org/docs/syntax#type-aliases (dosen't actually say what they are),

from reading online I figured its for defining types which are based on other built in types (not that I'm sure why its required)

j-panasiuk commented 8 years ago

This is a simple case of accidentally using type alias instead of type

type alias GameState
  = Started
  | Paused

Current error message is not helpful:

-- SYNTAX PROBLEM ---------------------------------------------

I ran into something unexpected when parsing your code!

2|     | Paused
       ^
I am looking for one of the following things:

    end of input
    whitespace

Maybe something like this instead?

-- SYNTAX PROBLEM ---------------------------------------------

I ran into something unexpected when parsing your code!

0|   type alias GameState
1|     = Started
2|     | Paused
       ^
It looks like you intend to create a new type, but you define it as a type alias.
You should probably use this instead:

0|   type GameState

(Read more about differences here blabla)

This is trivial once you know the difference between the two, but it's one of the things beginners may run into quickly. And in this case I think use of "|" makes user's intention clear