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

Optional positional args and the need for unit #625

Closed
chriswilty opened this issue Dec 13, 2022 · 3 comments
Closed

Optional positional args and the need for unit #625

chriswilty opened this issue Dec 13, 2022 · 3 comments

Comments

@chriswilty
Copy link

https://rescript-lang.org/docs/manual/latest/function#optional-labeled-arguments

There are a couple of things about optional args that are not coder-friendly:

  1. Positional args cannot be optional (even if there is just one arg)
  2. The need for "unit" param/arg as a final positional arg even when all args are labelled

Is there no way to make this a nicer experience? Mixing positional and labelled args in general seems a slightly odd allowance, but particularly so if it is causing this "unit" oddness. A necomer to the language (e.g. me a couple of weeks ago) is not going to understand why this is necessary:

Note for the sake of the type system, whenever you have an optional argument, you need to ensure that there's also at least one positional argument (aka non-labeled, non-optional argument) after it. If there's none, provide a dummy unit (aka ()) argument.

The need for "a dummy unit argument" is precisely the kind of quirk that could turn people away from this language. We come here in search of something better than typescript, so if this is going to remain in the language, I think we deserve a more detailed explanation than "for the sake of the type system". What is the trade-off here, what are we gaining in exchange for needing to use the dummy unit arg?

@chriswilty
Copy link
Author

I would label this a Question, but no labels are available ;)

@cristianoc
Copy link
Contributor

This can be explored in the context of uncurried types, where the number of arguments is fixed. So if an optional argument is omitted, the intention is clear.
With curried types, the intention is ambiguous, maybe the intention is to provide that extra argument later. In general, even a simpler example is confusing, one with only say 3 mandatory labelled arguments (~x, ~y, ~z) => x+y+z. If you only supply z, that is interpreted as partial application, returning a function that still expects x and y. This is going to surprise a number of users who simply forgot to pass extra mandatory arguments.
With uncurried types, passing only z is an error.

Uncurried types by default are explored in compiler v11, under development.

@cristianoc
Copy link
Contributor

See here rescript-lang/rescript#5907

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

No branches or pull requests

2 participants