Created
July 1, 2014 02:01
-
-
Save vtjnash/193c9d4bb5c11dd40a8d to your computer and use it in GitHub Desktop.
nargsout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# some definitions: | |
immutable NArgsOut{N} | |
NArgsOut() = ((isa(N,Int) && N>=-1) || error("nargsout must be ≥ -1"); new()) | |
end | |
nargsout(N::Integer) = NArgsOut{int(N)}() | |
# null behavior example: | |
f(::NArgsOut{-1}) = f() # -1 is a sentinal value and should have no effect on the output | |
f(::NArgsOut{0}) = (f(); ()) # 0 means the return value is ignored | |
f(::NArgsOut{1}) = (x=f(); s=start(x); (next(x,s),)) # want exactly one value to unpack | |
f(::NArgsOut{2}) = (x=f(); s=start(x); (next(x,s),next(x,s))) # want exactly two values to unpack | |
f{N}(::NArgsOut{N}) = f(nargsout(N+1)) # always valid to call a method defined for more NArgsOut | |
#alternative: f{N}(::NArgsOut{N}) = f() # in general, setting nargsout has no effect | |
# optimized behavior example: | |
g() = (1,2,3,4,5) | |
g(::NArgsOut) = g() | |
g(::NArgsOut{0}) = () | |
g(::NArgsOut{1}) = (1,) | |
g(::NArgsOut{2}) = (1,2,) | |
# special syntax parsing: | |
a = g() # calls g() | |
() = g() # calls g(NArgsOut{0}) if applicable, otherwise g() | |
(a,) = g() # calls g(NArgsOut{1}) if applicable, otherwise g() | |
(a,b) = g() # calls g(NArgsOut{2}) if applicable, otherwise g() |
I’m a bit amused you found this. It was early stage design work, and I decided to reject this concept entirely (for that, and numerous other reasons).
I’m a bit amused you found this. It was early stage design work, and I decided to reject this concept entirely (for that, and numerous other reasons).
when I need something I go to the darkest corners of the world to find it 😄 😉
Instead, we can have a macro (during definition and calling) that counts the number of outputs and adds arbitrary hidden Bool inputs.
calling:
@varargout out1, out2 = f(in1)
that is translated to
out1, out2 = f(in1, out1::Bool, out2::Bool)
definition:
# 2 meaning 2 output
@varargout 2 function f(in1)
....
end
that is translated to
function f(in1, out1::Bool, out2::Bool)
....
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if our
g
function has inputs itself?