Re: pbr vs multiple returns
- Posted by Tommy Carlier <tommy.carlier at telenet.be> Jan 11, 2007
- 583 views
Chris Bensler wrote: > I would definitely agree that PBR shoud be defined by both the callee and > caller. > I'm not very convinced that PBR is nessecary though. > > Other than convenience and a slight improvement to readability (which is > debatable > IMO), can someone please explain how PBR would be useful compared to multiple > return values? It aint clicking for me. Optimization? > > Also, regarding multiple return values. What is the purpose of providing an > alternate syntax for multiple return values? > I don't see the purpose of introducing a new syntax to declare that a function > returns multiple values. We can already do that by returning a sequence. All > we need is a syntax for how to assign the members of a returned sequence to > multiple variables. > > {err,val} = get("3.14") > > Shouldn't that be a viable statement without having to modify how get() works? > > In anycase, I would whole heartedly agree with the syntax I demonstrated for > multiple return values. I would rather not be required to use a special syntax > to declared that a function returns multiple values. I don't see much point > in it other than to force the caller to assign the result to multiple > variables, > instead of to a sequence if they wanted. I also think the assignment of multiple values would be great. It would make Euphoria more functional. The stack code could look like this:
-- pushes o onto stack, and returns stack function push(sequence stack, object o) return append(stack, o) end function -- pops an object from the stack, and returns {stack, popped_object} function pop(sequence stack) return { stack[1..$-1], stack[$] } end function sequence s object o s = {} for i = 1 to 10 do s = push(s, i) end for for i = 1 to 10 do {s, o} = pop(s) end for
That said, I also like the idea of PBR. I agree with Matt, that both the caller and callee have to specify the passing by reference. I don't really like the * notation of C/C++ (and OOEU?). I quite like the way it's done in C#, via the 'ref' keyword. In Euphoria, it could look like this:
procedure swap(ref object o1, ref object o2) object o o = o1 o1 = o2 o2 = o end procedure integer a, b a = 1 b = 2 swap(ref a, ref b)
The stack code could look like this, with PBR:
procedure push(ref sequence s, object o) s = append(s, o) end procedure function pop(ref sequence s) object o o = s[$] s = s[1..$-1] return o end function sequence s object o s = {} for i = 1 to 10 do push(ref s, o) end for for i = 1 to 10 do o = pop(ref s) end for
To be honest, I actually like this PBR -- The Internet combines the excitement of typing with the reliability of anonymous hearsay. tommy online: http://users.telenet.be/tommycarlier tommy.blog: http://tommycarlier.blogspot.com