Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
spec for #3662 and #3609
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Nov 16, 2014
1 parent b4e7d8b commit 5510847
Showing 1 changed file with 60 additions and 19 deletions.
79 changes: 60 additions & 19 deletions typechecker/en/modules/expressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1419,31 +1419,72 @@ Digit{1,2} ":" Digit{2} ( ":" Digit{2} ( ":" Digit{3} )? )?

<section id="inlineconditionalexpressions">
<title>Inline conditional expressions</title>

<comment>
<para>We plan to support inline <literal>if/then/else</literal> conditional
expressions, for example:</para>
<programlisting>Integer port = if (exists setting = process.propertyValue("port"))
then parseInteger(setting) else 8080;</programlisting>
<para>Note that this is more powerful than the <literal>then</literal> and
<literal>else</literal> operators because it allows all kinds of conditions,
not only boolean conditions.</para>
<para>Should we also support:</para>

<para>A conditional expression comes in two forms:</para>

<itemizedlist>
<listitem>
<para>inline <literal>switch/case/else</literal> conditional
expressions, or even</para>
<para>an <literal>if/then/else</literal> expression resembles
the <literal>if/else</literal> conditional defined in
<xref linkend="ifelse"/>, and</para>
</listitem>
<listitem>
<para>inline <literal>try/catch</literal> exceptional conditions?</para>
<para>a <literal>switch/case/else</literal> expression resembles
the <literal>switch/case/else</literal> conditional defined in
<xref linkend="switchcaseelse"/>.</para>
</listitem>
</itemizedlist>

<synopsis>ConditionalExpression: IfElseExpression | SwitchCaseElseExpression</synopsis>

<para>An <literal>if/then/else</literal> expression has a condition
list, as defined in <xref linkend="controlstructureconditions"/>, a
<literal>then</literal> expression, and an <literal>else</literal>
expression. The <literal>else</literal> expression is not optional.</para>

<synopsis>IfElseExpression: "if" ConditionList ThenExpression ElseExpression</synopsis>

<para>The type of an <literal>if/then/else</literal> expression with
<literal>then</literal> expression of type <literal>X</literal> and
<literal>else</literal> expression of type <literal>Y</literal> is
<literal>X|Y</literal>.</para>

<para>A <literal>switch/case/else</literal> expression has a
<literal>switch</literal> expression, a list of <literal>case</literal>
expressions, and, optionally, an <literal>else</literal> expression.</para>

<synopsis>SwitchCaseElseExpression: "switch" "(" Expression ")" CaseExpression ElseExpression</synopsis>

<para>The type of an <literal>if/then/else</literal> expression with
<literal>case</literal> expressions of type <literal>X1</literal>,
<literal>X2</literal>, ..., <literal>Xn</literal> and
<literal>else</literal> expression of type <literal>Y</literal> is
<literal>X1|X2|...|Xn|Y</literal>.</para>

<synopsis>ThenExpression: "then" Expression</synopsis>
<synopsis>ElseExpression: "else" Expression</synopsis>
<synopsis>CaseExpression: "case" "(" Case ")" Expression</synopsis>

<para>The expression following <literal>then</literal>,
<literal>else</literal>, and <literal>case</literal> is parsed with
precedence just higher than the <literal>||</literal> operator, and
just lower than the <literal>then</literal> and <literal>else</literal>
operators, that is, between the layers 3 and 4 defined in
<xref linkend="operatorprecedence"/>.</para>

<para>Each <literal>case</literal> expression includes a value case or
type case, as defined in <xref linkend="switchcaseelse"/>. Just like
in a <literal>switch/case/else</literal> conditional statement:</para>

<itemizedlist>
<listitem>
<para>all cases must be disjoint, and</para>
</listitem>
<listitem>
<para>if there is no <literal>else</literal> expression,
the cases must be exhaustive.</para>
</listitem>
</itemizedlist>
<para>For example:</para>
<programlisting>Float evaluated => switch (expr)
case (is Literal) expr.integer
case (is Plus) expr.left.evaluated + expr.right.evaluated
case (is Times) expr.left.evaluated * expr.right.evaluated;</programlisting>
</comment>

</section>

Expand Down

0 comments on commit 5510847

Please sign in to comment.