Let a wildcard as an actual type argument indicate a request for inference #3963
Labels
feature
Proposed language feature that solves one or more problems
type-inference
Type inference, issues or improvements
This is a proposal for supporting expressions like
MyClass<int, _>('Hello')
, where_
is used to indicate that the corresponding type parameter should be obtained from type inference. This allows us to specify some type arguments whose value would otherwise not fit the needs, and omit other type arguments whose inferred value is as desired. For example:We could allow a wildcard type argument that occurs as the last element of the actual type argument list to stand for multiple type arguments. For example, this would allow us to use
C<T1, _>
to stand forC<T1, T2, T3>
where bothT2
andT3
are inferred.There should not be a large amount of expressive power in this feature, but it does take a few steps to emulate it in the current language.
We can get the same effect today if we're willing to create a type alias for each choice of fixed type arguments, and if those type arguments can be denoted globally:
We can also use a function to support arbitrary choices of "fixed" type arguments:
It is a new feature to include a special (wildcard-ish) treatment of
_
when it is used as an actual type argument. It is also (slightly) breaking because it is possible today to declare some entities whose name is_
(e.g., top-level variables or, indeed, type parameters).However, I think it's more helpful to allow developers to request this kind of partial type inference by means of
_
than it is to preserve the ability to use a type whose name is_
as an actual type argument.In the end, it would actually be possible to create a type alias to provide access to such types under a different name:
The text was updated successfully, but these errors were encountered: