Skip to content
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

Optional positional type parameters #4219

Open
FMorschel opened this issue Dec 30, 2024 · 3 comments
Open

Optional positional type parameters #4219

FMorschel opened this issue Dec 30, 2024 · 3 comments
Labels
request Requests to resolve a particular developer problem

Comments

@FMorschel
Copy link

I saw this post on X.

The post has this code example:

// Optional error type with default
sealed class Result<T, [E extends Exception]> {...}

// `E` defaults to `Exception`
Result<User> getUser() {...}

// Custom error type
Result<User, ApiError> getUser2() {...}

Similar to our current examples of optional positional parameters, we could have an optional type parameter.

This would make code bases with strict_raw_type: true have maybe less work when the extended type is always the expected type (if we agree that for example it could have a lint similar to avoid_redundant_argument_values).

@FMorschel FMorschel added the request Requests to resolve a particular developer problem label Dec 30, 2024
@FMorschel
Copy link
Author

Somewhat related to #4124.

@pierremrtn
Copy link

I'm the author of the post, honestly I'm not sure if it's a good idea or worth the effort.
But following the same idea, we can imagine named generics which would allow arbitrary parameter ordering and optional types as well.

class MyClass<{A, B  extends Object, C extends num = int}> {}
 
final v = MyClass<A: SomeType1, B: SomeType2, C: double>();
final v2 = MyClass<B: SomeType2, A: SomeType1>(); // c default to int, params use arbitrary order

@FMorschel
Copy link
Author

Also related to #3837

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

2 participants