Skip to content

Conversation

leafpetersen
Copy link
Member

This is an alternative approach to definite assignment based promotion derived as a simplification of the definite assignment promotion proposal here. It eliminates the use of upper bound at join points, thereby enforcing the property that an untyped variable is never assigned more than one inferred type, except on paths that never join.

variable which is ascribed no **initialization inferred types** on any path
shall be treated as having declared type `Never`. Note that despite the
requirement that the **initialization inferred types** be equal at join points,
it is possible for a variable to take on different **initialization inferred
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could alternatively specify it to be an error if a variable ever takes on different initialization inferred types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would still allow a single type to be used, but anything with subtypes needs to be declared. Initializing to 0 and 1.0 would be invalid, not null (and avoid the LUB).
If you initialize with two different Widget subclasses, you need to say Widget on the variable.

It does mean that var x; if (test) { x = e1; } else {x = e2;} doesn't work like var x = test ? e1 : e2;, but there are so many ways it can not do that, and I'm not particularly fond of our LUB to begin with.

It could probably work. And it's a safe initial behavior, we can always improve later if we make it an error now.

```

Local variables with no explicitly written type but with an initializer are
given an inferred declared type equal to the type of their initializer. In the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this "inferred declared type" considered a "type of interest"?

From the description of initializing assignment, where we say that it is "treated ... as declared with the assigned expression as its initializing expression", and then we say that it is a type of interest, it seems that the answer is "yes". But I'd like to know for sure.

At a join point in the program, if an **untyped variable** has been given an
**initialization inferred type** on more than one of the reachable paths to the
join point and all of such **initialization inferred types** are syntactically
equal, the variable is treated after the join point as having that
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Syntactically equal before or after normalizaion?

Copy link
Member

@eernstg eernstg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer this model over the previous one, appreciating the simplification for developers that follows from the "no LUB" decision.

They will now be able to look up any of the the initializing assignments and trust that this is the source of the pseudo-declared type of the variable, rather than needing to look up all of them and computing a LUB (which is tricky in its own right) for all the join points.

@lrhn
Copy link
Member

lrhn commented Jun 2, 2020

Agree. Let's do this.

@leafpetersen
Copy link
Member Author

Closing this, we took a different approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes nnbd NNBD related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants