Forums

idea: multiple return values

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

idea: multiple return values

Postby kobi7 » Thu Nov 22, 2012 5:25 am

1) what do you think of multiple return values, like in the feature of "Multiple Target assignment"?
it might be easier than wrapping in pairs and tuples or a specific struct.

(I'm learning the hard way keeping to the minimal types)

an example signature would be
def next2(ints as List<of int>) as int, int

the calling side:
a, b = .next2(numbers)

I am not sure if it's a good idea or not. what do you think?

2) another question is with regard to delegates.
to define a method that takes another method, in C# I usually use the Func<,...> or Action<> as a parameter, and later call it with a lambda or a method.
It's unclear to me how to define such a signature in Cobra.
can you clear that up please? (some examples in the doc would do nicely)

thanks alot, kobi
Falun Dafa is Good.
Truth, Compassion, Forbearance is Good.
kobi7
 
Posts: 82
Location: Israel

Re: idea: multiple return values

Postby nerdzero » Thu Nov 29, 2012 9:51 am

1) Agreed, I would like to have multiple return values as well. You don't need it very often but when you do it's nice to have.


2) You can do this with the 'sig' and 'do' keywords.

http://cobra-language.com/trac/cobra/wiki/Delegates

http://cobra-language.com/trac/cobra/wiki/AnonMethod

If it's not clear, I can point out an example in the folding code for the MD addin.
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: idea: multiple return values

Postby hopscc » Tue Dec 11, 2012 6:19 am

Seems a small amount of syntax sugar used only occasionally
(that might possibly change if it was freely available though...
another plus is that the language could then lose the out and inout modifiers on fn args and parameters)

as noted
Dual value return types you can almost do simply now
using Lists if you can handle sloppy Typing or an explicitly typed Pair on the return values

a,b = .fn(123, 'xyz')
assert a.typeOf == int
assert b.typeOf == String

def .fn(i as int, s as String) as Pair<int, String>
# lotta calc
return Pair<int, String>(i, s)

def .fn(i as int, s as String) as List<of dynamic>
# lotta calc
return [i, s]

# vs
def .fn(i as int, s as String) as int, String
# lotta calc
return i, s


easier eventually if support a builtin literal Tuple type natively ( .Net 4?)
cf list [], dict {:} using some syntax like &[] (say ( *[] ??)

def .fn(i as int, s as String) as Tuple<of int, String>  # or some aliasing like (*) Stream -> as &[int, String]
# lotta calc
return &[i, s]


It is definitely cleaner appearing without the decl or punctuation cruft though.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: idea: multiple return values

Postby hopscc » Sun Jan 20, 2013 7:27 am

The idea for this has been surprisingly persistent in refusing to drop from my mind.

I opened ticket:315 as a placeholder for it.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: idea: multiple return values

Postby hopscc » Fri Jan 25, 2013 5:27 am

Theres a patch for an implementation of this on the ticket.
Syntax as given earlier - comma sep types on decl, comma sep values on return stmt.

It uses/relies on a new RTL container - Bag - which is an explicitly typed element holder (of up to 4 items)
based on Pair (2 element) - or eventually perhaps a typed Tuple ( > Net 4.?),

If just the Bag was provided in the RTL, code such as
a,b = .fn
assert a.typeOf == int
assert b.typeOf == String

def .fn as Bag<of int, String>
i as int
s as String
...
return Bag<of int, String>(i, s)

could easily implement fully typed multi-return value methods (at the price of some additional punctuation boilerplate)
which may be sufficient for most uses....
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand


Return to Discussion

Who is online

Users browsing this forum: No registered users and 92 guests

cron