-
-
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
RFC: Provide a way to get the inferred return type of a function #6692
Conversation
Implements Base.return_type(function, types) to get the inferred return type of a function for a given set of input types. Type inference knows about Base.return_type, and when the function and types are known at compile time, the call can be eliminated entirely.
If we have this, it'd be great to have the corresponding |
Looks like this might have been developed outside of Base, and has some module scoping which is not necessary? But this seems pretty intriguing. |
This would also be cool to have for tooling like TypeCheck.jl and similar projects that might pop up in the future. |
Is this the same functionality as @JeffBezanson's suggested |
If I understand correctly, |
This is stricter than necessary but should work.
What happens in the pathological case? F() = return_type(F, ()) |
It looks like type inference runs four times and then spits out |
How about other similarly foolish options then: type Z{T} end
f{T}(::Type{Z{T}}) = return_type(f, (Z{T-1},)) + return_type(f, (Z{T-2},))
f(::Type{Z{1}}) = Z{1}
f(::Type{Z{2}}) = Z{1}
+{A,B}(::Type{Z{A}}, ::Type{Z{B}}) = Z{A+B} |
Maybe the last three lines should actually construct the types? In any case, maybe I am missing something, but I'm not sure why this is problematic since we don't do constant folding in inference. The first definition is inferred as |
If you construct the types, then you should be able to get rid of the uses of |
As @simonster mentioned, something like this will help with |
It seems that we are moving away from allowing type inference to affect return types, so this PR is probably not the right solution to many of the issues it was designed to solve. I'm going to close it, but I'll leave the branch around for a bit just in case we want to revisit it. |
|
This PR implements
Base.return_type(function, types)
to get the inferred return type of a function for a given set of input types. Type inference knows aboutBase.return_type
, and when the function and types are known at compile time, the call can be eliminated entirely.This would make it possible to provide type inference for memoized functions, and it is the proper way to provide the right typeassert for the FFT inverse in #6193. It could also be used to determine the element type of the returned array for
map
andbroadcast
(see also #210, #670, #2938, #4883, #6548, and others), although formap
the overhead might be undesirable, and there is some debate over whether making the concrete output types of functions depend on type inference is a good idea.I am not entirely confident in the correctness of this code, so if this functionality is useful enough to include it, please take a close look.