x = if (y) "Text" else 8L # or equivalently: if (a) x = 4+2i else x = 8L
This example is the simplest case of types changing due to an if-statement. Here we will focus on the second form, because the first form can always be rewritten as the second, and the second does not emphasize any one assignment (there may be several assignments in the body of an if-statement).
Assuming that x
previously had type x#1
:
if
branch is bound to x#2
.x
is set to x#2
.The constraint x#2 <=> ComplexType
is added.
The write operand in the else
branch is bound to x#3
.
x
is set to x#3
.The constraint x#3 <=> IntegerType
is added.
At the end of the if-statement, the reaching type of x
is set to x#4
.
x#4 <=> RuntimeType(x#2, x#3, node)
is added, where node
is a reference to the If
node in the AST.In most cases, there will be more than one assignment. For example:
if (a) { x = 4+2i y = 12.0 } else { x = 8L y = 12L }
The steps are similar to above, but must be carried out for both x
and y
.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.