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

Multi return functions as expressions. #88

Open
bfish713 opened this issue Dec 12, 2014 · 6 comments
Open

Multi return functions as expressions. #88

bfish713 opened this issue Dec 12, 2014 · 6 comments

Comments

@bfish713
Copy link
Contributor

So I have small issue with calling functions. If a function returns multiple values in general It will not be allowed in an expression because we have no notion of tuples.

However a function call to a func returning 2 values could be used as 2 args or as 2 return values.

(int a, string b) = foo(1, 2); //valid rapid

x = y + foo(1, 1) // doesn't matter what x or y are I think this should always fail

// what happens in these?
too(boo()); // is this allowed
func a() int, string{ return boo()  } // How about this?
boo(); // I think this should be allowed
/*but*/ int a = boo(); //should not be

func too( int i, string s) {}
func foo(int x, string y) int, string{ return x, y; }
func boo() int, string { return 1, "hi" }

I think this needs to work with returns so we can do some easier recursion.
func boo(int x, int y) int, int{ return boo(x+1, y-1); }
I know this is infinite but syntactically makes sense to do compared to:
func boo(int x, int y) int, int{ (x, y) = boo(x+1 y-1); return x, y; }

@natebrennand
Copy link
Contributor

i don't think that we should do return value unpacking:

func foo(int x, int y) int, int{ return x, y; }
func boo(int x, int y) int, int{ return x, y; }
bool(foo(1,2))

Though it would be possible to do so, i just don't think it's worth our time right now.


I do think that we should allow functions to be used as expressions:

x = y + foo(1, 1)

This is pretty standard usage of functions to compose expressions.


agreed that calling a function w/o values to be assigned is fine.

@bfish713
Copy link
Contributor Author

So functions can be used in expressions but if it returns multiple values or void I don't think it should be allowed. Like int the above if foo returned 2 ints, it would fail but if foo returned just an int or string or whatever type x and y were it would compile. Is that fine?

@natebrennand
Copy link
Contributor

oops, i misread your example and assumed foo was a single return value.

agreed on all points.

@natebrennand
Copy link
Contributor

fwiw, i think we could get away with implementing the unpacking in function arguments since we can just call List.flatten on the types

@bfish713 bfish713 reopened this Dec 13, 2014
@bfish713
Copy link
Contributor Author

Does go support this in any way? because even if we could translate and check it might make code generation hard. I'm going to skip for now and if we have time I'll revisit

@natebrennand
Copy link
Contributor

// go supports this:
func foo(a, b int) (int, int) { return foo(a, b)}

// but not this
func bar(a, b, c int) (int, int) { return foo(a, b)}
bar(1, foo(1,2))

but we can make them both work

generated code:

// go supports this:
func foo(a, b int) (int, int) {
    x, y = foo(a,b)
    return x, y
}

func bar(a, b, c int) (int, int) {
    x, y = foo(a,b)
    return x, y
}

// bar(1, foo(1,2))
a, b = foo(1, 2)
bar(1, b, c)

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