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

Cannot make recursive functions with optional arguments. #1018

Open
smimram opened this issue Nov 1, 2019 · 4 comments
Open

Cannot make recursive functions with optional arguments. #1018

smimram opened this issue Nov 1, 2019 · 4 comments
Assignees
Labels

Comments

@smimram
Copy link
Member

smimram commented Nov 1, 2019

Consider the following code:

def rec fact(~n=0)
  if n == 0 then
    1
  else
    n * fact(n=(n-1))
  end
end

It gives rise to the error

Error 5: this value has type
  (n : _, ...) -> _ (inferred at line 7, char 8-13)
but it should be a supertype of the type of the value at line 3 char 0 - line 9 char 3
  (?n : _) -> _

because when the function fact is applied to the label n in the middle, we infer that it has a non-optional argument labeled n: we cannot guess that it is going to be optional yet...

This means in practice that we cannot make recursive functions with optional arguments (and I have more convincing examples than factorial...).

I don't see a way out without introducing a new syntax for using optional arguments when we really want to mean that they are optional. In OCaml, the way out is to use option types, which we don't have...

@toots any idea / comment ?

@smimram smimram added the bug label Nov 1, 2019
@smimram
Copy link
Member Author

smimram commented Nov 1, 2019

The way out consists in defining the function without optional arguments and the redefining it with optional arguments but this is not very clean...

@toots
Copy link
Member

toots commented Nov 13, 2019

That's an interesting one.. I think that for now we're stuck with it. Let's keep an eye on it.

@smimram
Copy link
Member Author

smimram commented Nov 13, 2019

BTW, it turns out that this is working in OCaml, which means that some magic is at work :)

@toots
Copy link
Member

toots commented Nov 13, 2019

Let's bring that to the magician @dbaelde..

giphy

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

No branches or pull requests

3 participants