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 function arguments #229

Open
snoopcatt opened this issue May 20, 2021 · 3 comments
Open

Optional function arguments #229

snoopcatt opened this issue May 20, 2021 · 3 comments

Comments

@snoopcatt
Copy link

Hello.
Let's summarize our discussion on that.

Nowadays Ravi lacks of support optional annotated function arguments.
For example, you can't do thing like that:

function sum(a: integer, b: integer, c: integer?)
  return a + b + (c or 0)
end

Of course, you can use any type, but then you have to add additional type check inside function to ensure that third argument is a integer.

Alternatively (or simultaneously☺), it can be implemented with Union types:

function sum(a: integer, b: integer, c: integer|nil)
  return a + b + (c or 0)
end
@dibyendumajumdar
Copy link
Owner

I prefer the former syntax - mainly because it is simpler to use, and also union types unless fully supported do not make sense.

@dibyendumajumdar
Copy link
Owner

There are some issues to consider. What would it mean for this:

local x: string

The reason this type is NIL or string today is that above is valid, and creates a local with NIL value to start with.
If we change this then every such local variable will need to be initialized to type specific default value.
Okay for strings maybe but what is the default value for userdata? Or function?

@dibyendumajumdar
Copy link
Owner

dibyendumajumdar commented May 22, 2021

Perhaps we should leave existing declaration semantics as union of NIL + type, but add new syntax for function arguments where stronger assertion is needed. Also @type operator could be strong assertion.

One possible solution is:

function x(s: string nonil) 
end

Or if @type assertion is made a strong one, then

function x(s @string) 
end

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

No branches or pull requests

2 participants