Make constructors for basic number types inferable #37213
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Starting from the assumption that a constructor should return an instance of the correct type, i.e.
T(...)::T
whereT isa Type
, I wondered: Does that hold for all types in julia proper? (Answer: no, e.g. ref. #37186 for an accidental violation I stumbled upon.) And can inference figure it out?This PR makes the answer to the second question "yes" for the basic number types, see the test:
julia/test/numbers.jl
Lines 2696 to 2700 in dca67c9
(There are two deprecated
BigFloat
methods which I haven't touched that would fail this test.)However, while that holds for the individual constructor methods, for unknown argument types, usually too many methods match, so that e.g.
Float64(x::Any)
(26 possible methods) will still only be inferred asAny
. So while I don't expect these changes to be wrong, I am not really sure how useful they are. Maybe @timholy has some intuition about it from all his work on invalidations where he looked at a lot code with incomplete type information?