-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
crazy idea: dispatch on return type #19206
Comments
One less "always first" argument makes also #19150 a bit cleaner. |
See also #10269 and @JeffBezanson's thesis discussion of arrow types. |
c.f. OpaqueClosures |
Do you have a specific issue we could we more about? |
From #37849 one can read :
Any information is welcome :) |
I certainly don't appreciate the challenge of automagic output type inference, but that would be a great and natural feature. My particular case is writing functions where the user may provide basic types or Unitful and needs particular return types to avoid type errors in later calculations. Ignoring any performance benefits from #37849, C-style overloading on input and output types keeps implementation details in the function definition and avoids subjecting users to unnecessary arguments or function name permutations for type consistency. imho. For clarity @vtjnash, is this closed because it is judged the same as #37849, blocked by it, it's old (2016), or just not on any roadmap? |
I couldn't find any discussion of this idea, so I thought I'd bring it up.
The idea is to introduce special syntax for the "desired return type" argument present in some functions along with special handling in dispatch. I haven't completely thought this through, but would like to stir up some discussion. Let me explain by example. I'll abuse function return type/type assert syntax, so please ignore the present meaning for now. (Syntax up for debate, of course.)
Note the reversed subtype matching compared to argument types, in that
foo()::Integer
invokesfoo()::Int
.That is, a function definition including
::T
promises that the function will return a value of (any subtype of) typeT
(and may well imply wrapping the return value inconvert(T,...)::T
to ensure that). A function call including::T
will invoke a method annotated with::S
such thatS<:T
. If there is more than one such method, the least specific (in the return type) one will win. So continuing the example:A function call without
::T
annotation is equivalent to::Any
. For function definitions I see two options, i.e.bar() = 0
could be equivalent tobar()::Any = 0
bar{T}()::T = 0
.With the first option,
bar()
returns anInt
, whilebar()::Int
disappointingly errors. With the second option,bar()::Int
would work (andbar()::Float64
could either fail a typeassert in0::Float64
or doconvert(Float64, 0)
). Dispatch involving type parameters as the return type would require more thought than I have invested up to this point, though.What would be the benefits? IMO, a dedicated syntax for the desired return type is much clearer than the current "return type or return type's element type or function that could be a constructor as first argument" convention:
(Yes, two of those do return a
Float64
.) Also, reversing the subtype relation in dispatch for the return type seems logical, although I'm not too sure this translates into any real benefits, as the current return type as argument approach does work.Note that I am not proposing to automagically infer the required return type of a function depending on the context where it is called.
The text was updated successfully, but these errors were encountered: