# How to return a parameterized type instance?

**URL:** <https://discourse.elm-lang.org/t/how-to-return-a-parameterized-type-instance/6907>\
**Category:** Learn\
**Created:** [February 13, 2021, 4:39am UTC](https://discourse.elm-lang.org/t/how-to-return-a-parameterized-type-instance/6907 "2021-02-13T04:39:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnQWang](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/johnqwang/32/3812_2.png) [@JohnQWang](https://discourse.elm-lang.org/u/JohnQWang)\
**Post date:** [February 13, 2021, 4:39am UTC](https://discourse.elm-lang.org/t/how-to-return-a-parameterized-type-instance/6907/1 "2021-02-13T04:39:48Z")

</div>

I’m learning ELM and got confused by the following code:

```auto
type MyType aa
  = Foo
  | Bar aa

-- case1: compiled
hi : Int -> MyType aa
hi ss = Foo

-- case2: not compiled
hi : Int -> MyType aa
hi ss =
  case ss of
    0 -> Foo
    _ -> Bar ss

```

My question is:

Why does the case2 not work? After all, it does return an instance of type `MyType aa` which comply with the function signature `hi : Int -> MyType aa`, regarding the case1 compiled even without the branch `Bar ss`.

---

<div class="post-metadata">

**Author:** ![DullBananas](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dullbananas/32/3709_2.png) [@DullBananas](https://discourse.elm-lang.org/u/DullBananas)\
**Post date:** [February 13, 2021, 4:45am UTC](https://discourse.elm-lang.org/t/how-to-return-a-parameterized-type-instance/6907/2 "2021-02-13T04:45:29Z")

</div>

Type annotations can’t be too general. It’s a `MyType Int`; it can’t be passed to a function that requires something else like `MyType Flont`. `MyType aa` is only valid if `aa` can be any type, such as here:

```elm
hi : MyType aa
hi =
    Foo

```

---

<div class="post-metadata">

**Author:** ![aszenz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/aszenz/32/3827_2.png) [@aszenz](https://discourse.elm-lang.org/u/aszenz)\
**Post date:** [February 13, 2021, 6:11am UTC](https://discourse.elm-lang.org/t/how-to-return-a-parameterized-type-instance/6907/3 "2021-02-13T06:11:44Z")

</div>

The return type of hi should be `MyType Int`, since u are returning a specific type instead of a generic one.

---

<div class="post-metadata">

**Author:** ![JohnQWang](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/johnqwang/32/3812_2.png) [@JohnQWang](https://discourse.elm-lang.org/u/JohnQWang)\
**Post date:** [February 13, 2021, 9:09am UTC](https://discourse.elm-lang.org/t/how-to-return-a-parameterized-type-instance/6907/4 "2021-02-13T09:09:55Z")

</div>

> The return type of hi should be `MyType Int`

Why is it _should_? I’m thinking the other way around: A function annotation is an (intentional) declaration to external world on what it’s input and output types, without any concern of its implementation at that moment. I’m going with the declaring-first-impimenting-later programming approach.

So by a declaration `hi : Int -> MyType aa`, I’m telling the world that my `hi` will return an instance of type `MyType aa`. That’s why my case1 worked as it did return an instance of `MyType aa`. I cannot understand why the case2 failed as it also return an instance of `MyType aa`. The differences of these two are their internal implementation which I think are not that important so long as they both comply with the annotation.

I belive my thinking is wrong somewhere, I need help to twist it around.

---

<div class="post-metadata">

**Author:** ![eike](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eike/32/749_2.png) [@eike](https://discourse.elm-lang.org/u/eike)\
**Post date:** [February 13, 2021, 10:11am UTC](https://discourse.elm-lang.org/t/how-to-return-a-parameterized-type-instance/6907/5 "2021-02-13T10:11:20Z")

</div>

The type variables in type annotations (i.e. the `aa` in your type `Int -> MyType aa`) are implicitly all-quantified. This means that the implementation of a function `Int -> MyType aa` must be valid for all choices of `aa` at once. In other words, the type `Int -> MyType aa` promises to produce a value of `MyType aa` for any value of `aa` that the _caller_ of the function wishes.

In case 1, your function is valid, because `Foo` can be a value of `MyType aa` regardless of what `aa` is. Your function `hi` in case 2 can’t do that: if `ss` is an `Int`, `Bar ss` has type `MyType Int`, but not `MyType String`.

---

<div class="post-metadata">

**Author:** ![JohnQWang](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/johnqwang/32/3812_2.png) [@JohnQWang](https://discourse.elm-lang.org/u/JohnQWang)\
**Post date:** [February 13, 2021, 10:29am UTC](https://discourse.elm-lang.org/t/how-to-return-a-parameterized-type-instance/6907/6 "2021-02-13T10:29:12Z")

</div>

You hit right at my pain point, thanks a lot!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [February 23, 2021, 10:29am UTC](https://discourse.elm-lang.org/t/how-to-return-a-parameterized-type-instance/6907/7 "2021-02-23T10:29:18Z")

</div>

Closed on February 23, 2021, 10:29am UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
