Skip to content

Commit

Permalink
Add section 'variant constructors are not functions'
Browse files Browse the repository at this point in the history
  • Loading branch information
feihong committed Apr 17, 2024
1 parent 0477948 commit 895386b
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions docs/burger-discounts/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,28 @@ Your code should now compile and all unit tests should pass. If you haven't done
so already, run `npm run promote` to promote the latest test output to become
the expected test output inside `tests.t`.

## Variant constructors are not functions

What happens if you to try to rewrite `Some(String.length("foobar"))`
to `"foobar" |> String.length |> Some`?


You'll get a compilation error:

```
Error This expression should not be a constructor, the expected type is int -> 'a
```

Variant constructors like `Some` are not functions, so they can't be used with
the pipe last (`|>`) operator. If you have have a long chain of function
invocations but you need to return a variant at the end, consider using an extra
variable, e.g.

<<< Discount.re#return-variant-at-end

See [full example on Melange
Playground](https://melange.re/v3.0.0/playground/?language=Reason&code=bGV0IGNpcGhlckdyZWV0aW5nID0gbmFtZSA9PiB7CiAgc3dpdGNoIChTdHJpbmcudHJpbShuYW1lKSkgewogIHwgIiIgPT4gTm9uZQogIHwgbmFtZSA9PgogICAgbGV0IHJlc3VsdCA9CiAgICAgIG5hbWUKICAgICAgfD4gU3RyaW5nLnNwbGl0X29uX2NoYXIoJyAnKQogICAgICB8PiBMaXN0Lm1hcChTdHJpbmcubWFwKGMgPT4gYyB8PiBDaGFyLmNvZGUgfD4gKCspKDEpIHw%2BIENoYXIuY2hyKSkKICAgICAgfD4gU3RyaW5nLmNvbmNhdCgiICIpCiAgICAgIHw%2BIFN0cmluZy5jYXQoIkhlbGxvLCAiKTsKCiAgICBTb21lKHJlc3VsdCk7CiAgfTsKfTsKCkpzLmxvZyhjaXBoZXJHcmVldGluZygiIikpOwpKcy5sb2coY2lwaGVyR3JlZXRpbmcoIlhhdmllciBMZXJveSIpKTsK&live=off).

---

Nice, you've implemented the burger discount, and you also understand more about
Expand All @@ -422,6 +444,7 @@ using lists, which are a better fit for this problem.
- Array → JavaScript array
- `None``undefined`
- `Some(value)``value`
- Variant constructors are not functions
- Array facts:
- Arrays are mutable, just like in JavaScript
- You can pattern match on arrays of fixed length
Expand All @@ -439,7 +462,7 @@ been filtered out. Refactor the function so that the "success" pattern match
looks like this:

```reason
| (Some(_), Some(cheaperPrice)) => Some(cheaperPrice)
| Some(cheaperPrice) => Some(cheaperPrice)
```

Also refactor the "failure" pattern match so there's no wildcard.
Expand All @@ -450,7 +473,6 @@ Use [Js.Array.map](https://melange.re/v3.0.0/api/re/melange/Js/Array/#val-map)

:::


::: details Solution

<<< Discount.re#improved-get-free-burger
Expand Down Expand Up @@ -494,28 +516,8 @@ to see how the tests are implemented. Note the use of a submodule to group the

:::

<b>4.</b> What happens if you to try to rewrite `Some(String.length("foobar"))`
to `"foobar" |> String.length |> Some`?

::: details Solution

You'll get a compilation error:
<b>4.</b> tbd

```
Error This expression should not be a constructor, the expected type is int -> 'a
```

Variant constructors like `Some` are not functions, so they can't be used with
the pipe last (`|>`) operator. If you have have a long string of function
invocations but you need to return a variant at the end, consider using an extra
variable, e.g.

<<< Discount.re#return-variant-at-end

See [full example on Melange
Playground](https://melange.re/v3.0.0/playground/?language=Reason&code=bGV0IGNpcGhlckdyZWV0aW5nID0gbmFtZSA9PiB7CiAgc3dpdGNoIChTdHJpbmcudHJpbShuYW1lKSkgewogIHwgIiIgPT4gTm9uZQogIHwgbmFtZSA9PgogICAgbGV0IHJlc3VsdCA9CiAgICAgIG5hbWUKICAgICAgfD4gU3RyaW5nLnNwbGl0X29uX2NoYXIoJyAnKQogICAgICB8PiBMaXN0Lm1hcChTdHJpbmcubWFwKGMgPT4gYyB8PiBDaGFyLmNvZGUgfD4gKCspKDEpIHw%2BIENoYXIuY2hyKSkKICAgICAgfD4gU3RyaW5nLmNvbmNhdCgiICIpCiAgICAgIHw%2BIFN0cmluZy5jYXQoIkhlbGxvLCAiKTsKCiAgICBTb21lKHJlc3VsdCk7CiAgfTsKfTsKCkpzLmxvZyhjaXBoZXJHcmVldGluZygiIikpOwpKcy5sb2coY2lwaGVyR3JlZXRpbmcoIlhhdmllciBMZXJveSIpKTsK&live=off).

:::

-----

Expand Down

0 comments on commit 895386b

Please sign in to comment.