Compiler: Docs on nested subtypes need example of unwrapping

Created on 27 Jul 2016  路  2Comments  路  Source: elm/compiler

Hi.

This is for ELM 0.17.x and the documentation on recursive eliminating recursive type aliases

Issue: the docs show how to avoid the compiler error but they do not show how to unwrap the nested subtype

The example from the docs is:

type alias Comment =
  { message : String
  , upvotes : Int
  , downvotes : Int
  , responses : Responses
  }

type Responses = Responses (List Comment)

All that is said re accessing the responses is _"...you only have to do some unwrapping in the cases where you are doing something recursive..."

This leads to, _"OK, but how do I do that?"_ 馃

Below that paragraph is needed an example of unwrapping the responses back to a List Comment.

I have something like this:

unwrapResponses : Responses -> List Comment
unwrapResponses responses =
    case responses of 
        Responses comments -> comments

I'm happy to do a PR, but is this the canonical answer? (It seems a little clumsy.)

Thanks.

request

Most helpful comment

When you define a union type with only one case, it's possible to do

unwrapResponses (Responses comments) =

Essentially, you're doing a pattern match in the argument list, and because there's only one you know it cannot fail. You can't use this trick, for example, on a Maybe; you'd use a case analysis exactly as you have above.

All 2 comments

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

When you define a union type with only one case, it's possible to do

unwrapResponses (Responses comments) =

Essentially, you're doing a pattern match in the argument list, and because there's only one you know it cannot fail. You can't use this trick, for example, on a Maybe; you'd use a case analysis exactly as you have above.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

avh4 picture avh4  路  3Comments

torepettersen picture torepettersen  路  4Comments

lmmarsano picture lmmarsano  路  4Comments

rhaps0dy picture rhaps0dy  路  4Comments

evancz picture evancz  路  4Comments