Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split current 'order with promo' chapter into two separate chapters #52

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

feihong
Copy link
Collaborator

@feihong feihong commented Sep 11, 2024

Most of the content is the same, just split up into two chapters. However, I did add a few extra sections and exercises.

Notably, I removed all the Effects material and will use it for a different application example.

Copy link
Member

@jchavarri jchavarri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I finished a review of the first of the two chapters 😌 Submitting because there were a few comments already, I will go with the other one tomorrow.

docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved
implement it with a normal variant instead of a polymorphic variant. This small
refactor should give us more insight into OCaml's type system. Additionally,
normal variants are better than polymorphic variants at "documenting" the types
that will be used in your program, since they must always be explicitly defined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice to mention documentation, but this doesn't explain much about the technical use cases where normal variants are more useful than poly variants:

  • Errors and signatures are simpler
  • Stronger type guarantees (pattern match "completeness", safer refactoring)
  • Potentially better performance (both at build time and runtime)

The downsides are that:

  • Normal variants can be "expanded" dynamically, so for cases where it's unclear if more cases might be needed (or where the author of the types don't control their usage, like library authors) polyvars might fit better because extra flexibility
  • They always need to be namespaced with module or annotated explicitly when used, while polyvars values can just be written without any annotations

Do you think it's worth mentioning these aspects?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it's worthwhile to go over these points. I prefer to bring them up one by one and then summarize at the end, but we'll see how it goes 🤞

Copy link
Collaborator Author

@feihong feihong Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors and signatures are simpler

Here, do you mean that error messages for normal variants are simpler?

Normal variants can be "expanded" dynamically, so for cases where it's unclear if more cases might be needed

Do you mean that normal variants cannot be expanded dynamically?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, do you mean that error messages for normal variants are simpler?

Yes.

Do you mean that normal variants cannot be expanded dynamically?

Yes, I said it the other way around 🙃

docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved
docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved
docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved
docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved

<<< Types.re#add-type-arg{1}

This is somewhat like accidentally using a variable in a function but forgetting
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something that helps me wrap my head around these arguments is that they will be used by the compiler to "carry" the information about which extra tags are being added to the open variant, on each usage.

docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved
docs/poly-to-normal-variant/index.md Show resolved Hide resolved
Comment on lines 219 to 220
In the next version of Melange, polymorphic variant definitions no longer
require a space between `[` and `|`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait :wat: is this because of some changes in the OCaml compiler, or Reason? In any case, I don't think this is Melange responsibility, so I would not add this warning unless it refers to the last package and version that didn't support it and the first one that will.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it was a Reason parsing bug, see reasonml/reason#2781. Not sure if that fix is in the current melange 4.0.1 release.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we shouldn't refer to Melange new version. Reason is independent package than Melange, and they can be combined in different ways. In other words, having Melange 4.0.1 doesn't involve getting the latest reason.

@feihong feihong marked this pull request as ready for review September 11, 2024 14:21

::: warning

In the next version of Melange, polymorphic variant definitions no longer
Copy link
Collaborator Author

@feihong feihong Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the current version already fix this issue? By that I mean 4.0.1-52.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #52 (comment). Melange version is independent from Reason version.


:::

<b>4.</b> When you enter a promo code that is blank (empty or consists of only
Copy link
Collaborator Author

@feihong feihong Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new exercise. It doesn't have much to do with types, but it allows me to not put too many exercises in the next chapter.

Comment on lines 219 to 220
In the next version of Melange, polymorphic variant definitions no longer
require a space between `[` and `|`.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it was a Reason parsing bug, see reasonml/reason#2781. Not sure if that fix is in the current melange 4.0.1 release.

Copy link
Collaborator Author

@feihong feihong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made a number of fixes but still thinking about how to work in "pros and cons" from https://github.com/melange-re/melange-for-react-devs/pull/52/files#r1754546821

docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved
docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved
docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved
docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved
docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved
docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved
docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved
docs/poly-to-normal-variant/index.md Outdated Show resolved Hide resolved
Copy link
Member

@jchavarri jchavarri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! It's still unclear to me how the convo about technical merits of polyvar vs normal variants will be resolved?

## Add `Promo` to `Order`

Add the `Promo` component to the `Order` component:

<<< Order.re#make{3,22-25,28}
<<< Order.re#make{3,5,18-25,28}

A breakdown:

- Create a new state variable called `discount` (along with its attendant
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never read "attendant" in this sense.


::: warning

In the next version of [Reason](https://reasonml.github.io/), polymorphic
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be tested here. Note that refmt still adds the space.

Re: "the next version", how do you plan on tracking a reminder or similar so that this chapter is updated when the next release is out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants