-
Notifications
You must be signed in to change notification settings - Fork 213
Remove type literals from the language. #2393
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
Comments
@lrhn, I'm building a server framework in pure dart to serve all front-end flavours (flutter, tailwindcss, vue, react, svelt etc). So the framework trust on mirrors to inject depencies based on types. For example if you define a controller like below, you will have the class UserController extends Controller {
updateAmount(int id, Request request, UserRepository repository) {
...
await repository.updateBillingAmount(request.get('amount'));
...
}
} I don't want to change this behavior but I'd like to know/understand how I should be able to do this without mirrors. What would be the best or official way to use reflections without mirrors? (Sorry If this question was massively asked dart team and if I'm missing something here. I'm new to dart language but I love it) I appreciate any help or suggestions on this. Thank you |
In this case, you may be interested in the up-and-coming macros feature, which makes code generation much easier to work with and officially supported by the language. |
I know this is a long shot.
Dart allows expressions like
int
andList<int>
to evaluate toType
instances representing those types.That's very convenient when you want to have a type literal, but with
dart:mirrors
being deprecated, andType
objects being useless for most things (all they support is equality), there shouldn't be that manyType
instances. In most cases, a type parameter is a better solution to the problem people are trying to solve usingType
objects. If anything, the syntactic ease of creatingType
objects is encouraging too many uses.The syntax of type literals has already gotten in the way of using the same syntax for something else. The
ClassName.new
tear-off syntax for tearing off the unnamed constructor was only introduced becauseClassName
was already taken by the type literal.With the constuctor tear-off language feature, Dart started allowing
List<int>
as a type literal too. That also allows us to define a type alias liketypedef typeof<T> = T;
, so thattypeof<any type>
can create aType
object for any type, even those not allowed as type literals.So, I suggest we introduce a canonical
typedef typeof<T> = T;
in the platform libraries, and disallowed any type literal other thantypeof<some type>
.All existing uses of type literals need to be migrated. That can be entirely automatized, but it does require running dart-fix on all the code.
We will be using language versioning, because the
typeof
syntax is backwards compatible, and we can start warning and hinting that you should fix the code a few versions earlier than when we actually remove the feature.All you have to do to migrate will be running
dart fix
. (And existing code will keep working at the prior language versions as long as we support those).This will allow us to:
ClassName
as the constructor tear-off.Type
objects. If you have your ownclass Type<T>
to represent types, it's shorter to write thantypeof<T>
, and can have more features.if (T is int)
to check if a type variable is some type, it instead gives no error and is always false).The text was updated successfully, but these errors were encountered: