@@ -0,0 +1,161 @@
1+# Hints for Recursive Type Aliases
3+At the root of this issue is the distinction between a `type` and a `type alias`.
6+## What is a type alias?
8+When you create a type alias, you are just creating a shorthand to refer to an existing type. So when you say the following:
11+type alias Time = Float
13+type alias Degree = Float
15+type alias Weight = Float
18+You have not created any *new* types, you just made some alternate names for `Float`. You can write down things like this and it'll work fine:
21+add : Time -> Degree -> Weight
26+This is kind of a weird way to use type aliases though. The typical usage would be for records, where you do not want to write out the whole thing every time. Stuff like this:
36+It is much easier to write down `Person` in a type, and then it will just expand out to the underlying type when the compiler checks the program.
39+## Recursive type aliases?
41+Okay, so lets say you have some type that may contain itself. In Elm, a common example of this is a comment that might have subcomments:
48+ , responses : List Comment
52+Now remember that type *aliases* are just alternate names for the real type. So to make `Comment` into a concrete type, the compiler would start expanding it out.
68+ , responses : List ...
74+The compiler cannot deal with values like this. It would just keep expanding forever.
79+In cases where you want a recursive type, you need to actually create a brand new type. This is what the `type` keyword is for. A simple example of this can be seen when defining a linked list:
87+No matter what, the type of `Node n xs` is going to be `List`. There is no expansion to be done. This means you can represent recursive structures with types do not explode into infinity.
89+So let's return to wanting to represent a `Comment` that may have responses. There are a couple ways to do this:
92+### Obvious, but kind of annoying
100+ , responses : List Comment
104+Now lets say you want to register an upvote on a comment:
107+upvote : Comment -> Comment
108+upvote (Comment comment) =
109+ Comment { comment | upvotes = 1 + comment.upvotes }
112+It is kind of annoying that we now have to unwrap and wrap the record to do anything with it.
115+### Less obvious, but nicer
122+ , responses : Responses
125+type Responses = Responses (List Comment)
128+In this world, we introduce the `Responses` type to capture the recursion, but `Comment` is still an alias for a record. This means the `upvote` function looks nice again:
131+upvote : Comment -> Comment
133+ { comment | upvotes = 1 + comment.upvotes }
136+So rather than having to unwrap a `Comment` to do *anything* to it, you only have to do some unwrapping in the cases where you are doing something recusive. In practice, this means you will do less unwrapping which is nice.
139+## Mutually recursive type aliases
141+It is also possible to build type aliases that are *mutually* recursive. That might be something like this:
148+ , responses : Responses
151+type alias Responses =
153+ , responses : List Comment
156+type SortBy = Time | Score | MostResponses
159+When you try to expand `Comment` you have to expand `Responses` which needs to expand `Comment` which needs to expand `Responses`, etc.
161+So this is just a fancy case of a self-recusive type alias. The solution is the same. Somewhere in that cycle, you need to define an actual `type` to end the infinite expansion.