new language feature ?

hi to all,

there is a language feature I would really like to have in xojo. multiple return values.
I know you can pass parameters byref and have the feature, but I find this confusing
mainly because you dont know if you have a lot of parameters which one is returned or not.

so a syntax like :

(value1,value2,...,valuen) = mymethod( param1,...,paramn)

and of course in the method :

return (value1,..,valuen)

would be far more productive and clear reading to me than the other with byref parameters.

what do you think ?
and you, xojo folks, is it something easy to add ? or really not at the plan for the moment 


thanks.

1 Like

Why not just return an array?

you have to deal with the array after passing to the method, and it’s not “easy” as to have the values in each variable
most of the times I would only have 2 may be 3 return values, never a big size (where I would use arrays)

Use a dictionary, JSONItem or a custom class (this allows auto complete in the IDE too)
 I think pretty much every language requires you to either use ByRef to “return” multiple values or to return an array


old “original” oop programming languages used to have multiple return values syntax without byref
Lisp, prograph, dylan, among others (ada also I think ?)

So they do, but Xojo does not
 You can submit it as a feature request, but in the meantime, you will have to use what the framework gives you


swift does it too 


[quote]Functions with Multiple Return Values
You can use a tuple type as the return type for a function to return multiple values as part of one compound return value.
The example below defines a function called minMax(_:), which finds the smallest and largest numbers in an array of Int values:
[/quote]

[code] func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array[1
<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}

[/code]

Not exactly what I call the OOP languages.

  • To LISP OOP features were added some 20 years after the language was invented.
  • Prograph is not a 3rd generation programming language AFAIK.
  • Dylan has its roots in LISP and was one of the attempts to mingle OOP into LISP while modernizing LISP at the same time. But Dylan is not really alive (which is sad – I love this programming language!).

Simula as first OOP language did not have multiple return values. Smalltalk, C / C++, C# / VB .NET, Objective C, ALGOL/Pascal-based OOP languages (which includes Ada) all do not support multiple return values. Java neither as far as I know.

All languages which have procedural roots do not support it due to how compilers work. Over the time as compiler development has evolved, this has changed of course, and for example Swift – which has been heavily influenced by non-procedural / non-OOP languages – supports it (which means the LLVM-team must have introduced that feature at some point).

Personaly I have never missed this feature. But you could use Pair for two return values. Or you could create your own class.

I dont want to use a pair or a class or an array to do that.
because you have to assign the values to the array/pair/class before
and then get the values back into the variables after the method
that’s at least 2 lines per variable
with the “new” syntax it’s all in one line.

So given ‘what works now’, pass (n) variables byRef and have the method amend them instead of returning them.

Return an array or a dictionary.

[quote=199699:@jean-yves pochez]and of course in the method :

return (value1,
,valuen)
would be far more productive and clear reading to me than the other with byref parameters.[/quote]

Seems to me byRef is the easiest, but I have also used structures and arrays.

At any rate, why don’t you simply file a feature request, if you want that ?

Why doesn’t returning a class work just as well?

it would be mainly to return a set of integers, strings, doubles.

The main reason IMHO against multiple return values is: if the data returned is related to each other it should be an object, an array, or a dictionary. If it is not related to each other, it should not be returned by the same function, but by two or more functions returning each value by itself (Data Clump)

By the way, it is the same with out parameters: one is acceptable, two or more are not. A valid case in C is for example outCount as an “out parameter” while the function returns the pointer to the array.

We have stuff that will work with a little effort, no one is questioning that. But I think he phrased the question as a possible feature request. Must be some reason why Swift thought it was important enough to add, there are ways to workaround it in Swift too. Sometimes I wish I could get more than one return value back without returning a class or an array, would make things easier in some respects, but I am sure there are a lot more pressing feature requests.

A request like this already exists
<https://xojo.com/issue/33691>

Altering the language in this fashion is not “trivial”

I’d return a class instance even if all the class does is wrap up several values and has no methods
Somehow all those return values are related

Prograph is entirely different from every language I have ever used
It’s a data flow / object flow based language that uses a visual IDE
Still exists as Marten

fun reading
http://programmers.stackexchange.com/questions/96168/what-is-the-benefit-of-multiple-return-values

[quote=199791:@Norman Palardy]fun reading
http://programmers.stackexchange.com/questions/96168/what-is-the-benefit-of-multiple-return-values[/quote]

I like this one comment from that thread,

I think using a class, or other form of grouping the return values, and returning just a single value is probably better for forward compatibility (I know most people just think about backwards compatibility)
 Plus, by using a class (at least in Xojo), it gives you auto-complete in the IDE for the different properties and methods


[quote=199779:@Norman Palardy]Prograph is entirely different from every language I have ever used
It’s a data flow / object flow based language that uses a visual IDE
Still exists as Marten[/quote]

I loved Prograph and wrote my first major shareware app in it. To bad I was in a minority.