-
Notifications
You must be signed in to change notification settings - Fork 214
Opt-in type promotion - Analysis #4132
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
Using annotations won't be the way, they're traditionally not allowed to change language semantics. See #1518 for a possible syntax for ensuring that a getter is stable, and it's value can therefore be assumed to be the same between two different reads. The other two, more local, approaches require that the two reads of That's what a pattern check with binding does explicitly, introduce a local variable. I think it's unlikely that Dart will allow normal code to look like On the other hand, if the |
This issue is not trying to change the behaviour of getters or implicit type promotion. This issue is specifically about "what if i know that it can be promoted without sideeffects", where the user overrides the compiler issues with the promise that it won't cause a problem. Behaviour-wise it is similar to null assertion, which overrides the compiler telling it, that it can be null, despite the user knowing full well it's not. In other words, imagine if Dart didn't have null assertion operator, and you would need to assign new variable / cast it every time you wanted to use it as non-null value. This is the current behaviour with advanced type promotion, and this issue is about adding a sort of "promotion assertion" construct, sort of like null assertion The promotion would still check the types of the values and would probably (depending on specification) throw if the types don't match. This means that instead of user requesting from the language team creation of type promotion exception just for one very specific edgecase, and not being able to use it till the next patch, the user would just tell the compiler "trust me", and the compiler would promote it if it can (based on looser rules) |
The promotion that inserts type checks is not a promotion, You need another word for this. It would be easier to write This issue has been discussed for years. If you ask me, there's no good solution other than reinterpreting "final" to mean (by default): this thing stays final in subclasses. This is a potentially breaking change—there's no appetite for this at the moment. Other solutions are too complicated, IMO. |
@tatumizer you've misunderstood me. What i meant was it would do type checking when promoting, after that it promote the variable. It won't check how the variable is used. I've said this because i am in favor of the last syntax, which would allow promoting with explicit type, and so it would need to check if it can be promoted to the explicit type, not just the inferred one. promote String foo.value; Also, my proposal is not JUST about promoting nullable type, but also about promoting any sealed typed implicitly or any type explicitly. |
@hydro63 : your clarification doesn't make any difference. |
Exactly, since there is no guarantee, the compiler can't promote it. But what if there is information that the user knows, and the compiler doesn't, like for example, that the user won't be stupid and won't try to override final variables. What i want is to able to say to compiler that, i know there is no guarantee, but I guarantee it. I will be the guarantor, and so you can just promote it. If i'm somehow wrong, just let it crash. |
@hydro63 : what you are missing is: the "brittleness argument". |
I understand this, but i still think that it could be a good feature if used correctly. Still, thank you for your opinion |
But it will. It must. The Dart type system is sound. It's not always safe, meaning that there are things you are allowed to do that are not guaranteed to be type- correct at compile-time. In every such case, the compiler inserts a runtime check that will throw if the result of an expression doesn't have its static type. That makes the language sound in the sense that if an expression has static type What you are describing is a way for the author to tell the compiler to "trust me, I know what I'm doing". |
Considering that i started this feature analysis on wrong assumptions (aka Dart always does type-checking), and the fact that this feature is really not all that good with corrected knowledge, i think this issue should be closed (unless someone wants to add something) So i'm gonna close this issue. |
DISCLAIMER - this is not a feature request, just an analysis of possible feature
Context
Inspiration
This issue / thread is based on @lrhn comment on issue #4131, where he said, that for type promotion of immutable members of class to be possible, it would need to be opt-in (not language-wise), to make sure that the developer understand what he is doing, etc ... He talks about the developer needing to make a *promise*, that he won't do unsafe stuff, and as such, it can't just be implemented language-wise.This issue is trying to create a way for a developer to make a promise, and allow the developer to specify, when a certain thing is
promotable
, despite the compiler disagreeing. It is kind of like withnull assertion !
, where you say to compiler that you are sure that it's not null.Motivation
From what i've seen, significant portion of the issues / feature requests posted to the language repo talk about type promotion in some specific edgecase, when the developer is certain that it can be promoted without causing any trouble. This feature would make type promotion easier and faster to use, which would in turn make those developers at least a bit happier.
In current Dart, there are already mechanisms to do a type promotion - inline casting or assigning to new variable. The problem with both is that both are quite verbose and redundant, since we already know what type a variable is.
This feature would ideally make it easier to do explicit type promotion, and remove a lot of the bloat associated with types.
Issue in practice
Here is how the issue would work in practice:
Possible syntax
Here are the possible syntaxes i thought of, that would have the function of making a promise, that something can be promoted.
Annotation on member / field / variable
A specific variable would be annotated with the promise during it's definition
Annotation on block
The promotable variables would be listed before the block they were defined in.
promote
conditionThe
promote
condition would work the same as theif
condition, while also signifying the promise of promotability. If the condition fails, the content in the block will not be executed, nor will it throw. Instead it will work like a regularif
condition, and execute the statement after it.promote
statementThe
promote
statement would try to promote everything, that the user specifies. The specified fields / variables would be primarily promoted to their inferred type, with optional explicit typing allowed. The types would be inferred from the information leading up to thepromote
keyword, and the types would be promoted until the end of the block. If it can't promote, it will throw.Conclusion
I think that if this
opt-in promotion
would be ever implemented, or even taken seriously, i think that the last option (promote
statement) is the most viable solution and the best and most flexible syntax. It would allow both quick return / throw, if the types of the arguments don't match, and also quick promotion for the variables we are sure can be promoted safely.Still, as i said, this is not really a feature request, it's a feature analysis, so i'd appreciate your ideas for the possible syntaxes, or critisisms why it would be bad.
The text was updated successfully, but these errors were encountered: