Open GoogleCodeExporter opened 9 years ago
There are currently two ways that you can do this, they're both bad because
they are inefficient:
As you said, you can wrap the primitives in an object:
class Int { int i; oper init(int i) : i = i {} }
void toInt(Int x, Int y) { ... }
You could also use an array:
Array[int] arr = {2};
void toInt(arr) { ... }
(a low-level array would be slightly more efficient but also less safe0
I've been recently considering the question of whether we want to get into the
business of passing references because this feature would be useful for
bindings. For example, there are lots of C functions that accept pointers to
types for exactly the use-case you have described: output parameters. We
currently have to use arrays for these, which involves an allocation step. I
was considering adding the ability to use the '&' operator on an arbitrary
value in the same way that C uses it, the type of the result would be
"array[T]" (where T is the type of the argument). If we did this, your code
could be written as follows:
int x, y;
void toInt(array[int] x, array[int] y) { ... }
toInt(&x, &y);
This addresses the performance issues, but not the safety issues inherent in
the use of arrays. For example, toInt(null, null) would be perfectly valid but
result in a runtime error.
Alternately, we could allow the use of a reference argument like in C++:
int x, y;
void toInt(int &x, int &y) { ... }
toInt(x, y);
This could address both the safety and efficiency concerns because it would
require an actual l-value of the specified type. Unfortunately, it also
complicates our (currently very simple) type syntax and introduces the concept
of l-values, which will likely have their own problems.
Alternately, for the specific example you've given, we could take a page from
many other languages and allow multiple return values:
int x, y;
(x, y) = toInt();
Original comment by mind...@gmail.com
on 8 Dec 2011 at 5:52
[continuing where I left off prematurely]
In conclusion, I'm keeping this issue open for discussion. Crack devs and
users, please feel free to chime in.
Original comment by mind...@gmail.com
on 8 Dec 2011 at 5:55
My preference would be finding a solution involving multiple return values. I
think this is more intuitive and certainly more "scripting language" like.
Original comment by weyrick
on 8 Dec 2011 at 6:21
I also like the solution with multiple return values.
Something like tuple in Python would be nice:
def toInt():
return 5, 10
x, y = toInt()
Original comment by boua...@mailinator.com
on 8 Dec 2011 at 7:31
Yes, the multiple return values is the most elegant solution.
Original comment by thekara...@gmail.com
on 19 Oct 2013 at 5:25
Original issue reported on code.google.com by
boua...@mailinator.com
on 8 Dec 2011 at 4:42