Sum data type: Difference between revisions

m
→‎{{header|Phix}}: added explicit crash for p2js
m (→‎{{header|Phix}}: syntax coloured, added p2js note)
m (→‎{{header|Phix}}: added explicit crash for p2js)
Line 492:
User defined types can be used to enforce restrictions on the contents of variables.
 
Note however that JavaScript is a typeless language, so no error occurs under pwa/p2js on the assignment, but you can still explicitly check and crash, as shown.
 
<!--<lang Phix>(phixonline)-->
Line 519:
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">127</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- fine</span>
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"::c01e:fc9a"</span> <span style="color: #000080;font-style:italic;">-- fine</span>
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- error (but notno such error under p2js)</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">ipaddr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"however this works/crashes properly under p2js"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</lang>-->
 
7,815

edits