diff --git a/docs/burger-discounts/index.md b/docs/burger-discounts/index.md index 8ff59b95..3772707d 100644 --- a/docs/burger-discounts/index.md +++ b/docs/burger-discounts/index.md @@ -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 @@ -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 @@ -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. @@ -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 @@ -494,28 +516,8 @@ to see how the tests are implemented. Note the use of a submodule to group the ::: -4. 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: +4. 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). - -::: -----