Think about tuples. You can have a type like `(Int, Int)`, and you can use
it in your function annotations.

Now, you can reference this type with an alias too like `type alias
TwoIntsTuple = (Int, Int)`
TwoIntsTuple will not be a new type, it is the old type `(Int, Int)` that
you can now reference with a new name that makes more sense to you.

In the same way, you could use the record  { x : Int, y : Int } all over
your code OR, you can give it a name (alias) like `type alias Point = { x :
Int, y : Int}`

If you want different behavior, you hide the record behind a tag and make
sure that all the values are valid.

`type SafePoint = SafePoint { x : Int, y: Int}` or, using the above alias
`type SafePoint = SafePoint Point`

This way you are actually creating a new type. You can have functions over
this type that work ONLY on this type and not on all the  { x : Int, y :
Int}
You can be sure for example that x will never be negative or y will never
be over some number because you control all the operations.

Another point. All the unions in Elm are tagged unions, so, `type X  = T1
| T2` will only work if T1 and T2 are just tags. If they are types, you
need to tag them like `type X = A T1 | B T2`



On Thu, Aug 11, 2016 at 2:14 PM, Franček Prijatelj <
[email protected]> wrote:

> Hi
>
> I am asking why* type alias* instead of just *type*, to declare record.
>
> type X
>     = T1
>     | T2
>
>
> type alias X1 =
>     X
>
> *-- why not just type here*
> type alias Y =
>     { a : Int
>     , b : Int
>     }
>
>
> type alias Y1 =
>     Y
>
>
> Samples:
>
>
> a1  : X          -- Ok
> a2  : X1         -- Ok
> a3 : T1  | T2    -- Error
>
>
> b1 : Y                    -- Ok
> b2 : Y1                   -- Ok
> b3 : { a: Int , b : Int } -- Ok
>
>
>
> regards
> Franček
>
> --
> You received this message because you are subscribed to the Google Groups
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>



-- 
There is NO FATE, we are the creators.
blog: http://damoc.ro/

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to