Define a primitive data type: Difference between revisions

m
→‎{{header|Phix}}: added syntax colouring, marked p2js compatible
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
Line 1,877:
{{trans|Euphoria}}
In Phix types are special functions that may be used in declaring the allowed values for a variable. A type must have exactly one parameter and should return true (non-zero) or false (zero). Types can also be called just like other functions. The types '''object''', '''sequence''', '''string''', '''atom''' and '''integer''' are predefined.
<!--<lang Phix>type iten(integer iphixonline)-->
<span style="color: #008080;">type</span> <span style="color: #000000;">iten</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
return i>=1 and i<=10
<span style="color: #008080;">return</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">10</span>
end type</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">type</span>
<!--</lang>-->
You can then declare variables of the new type just as you would the builtins
<!--<lang Phix>integer i(phixonline)-->
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span>
iten i10</lang>
<span style="color: #000000;">iten</span> <span style="color: #000000;">i10</span>
<!--</lang>-->
and typechecking occurs automatically
<!--<lang Phix>i = 11 (phixonline)-- fine>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">11</span> <span style="color: #000080;font-style:italic;">-- fine</span>
i10 = 11 -- runtime error</lang>
<span style="color: #000000;">i10</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">11</span> <span style="color: #000080;font-style:italic;">-- runtime error (desktop/Phix only, not pwa/p2js)</span>
<!--</lang>-->
Note that standard practice is to get a program running and fully tested on desktop/Phix first, ''before'' using pwa/p2js to run it in a browser.
 
=={{header|PicoLisp}}==
7,796

edits