Define a primitive data type: Difference between revisions

(Add a Rust solution)
(review →‎Pascal)
Line 191:
===Other libraries or implementation specific extensions===
As of February 2009 no open source libraries to do this task have been located.
 
=={{header|C sharp|C#}}==
<lang csharp>public struct TinyInt
Line 286 ⟶ 287:
unsigned char value; // we don't need more space
};</lang>
 
=={{header|Clojure}}==
 
Line 429 ⟶ 431:
assert(j > i);
}</lang>
 
=={{header|Delphi}}==
''See [[#Free Pascal|Free Pascal]]''
 
=={{header|Dyalect}}==
 
Line 542 ⟶ 548:
❗ error: One-ten : type-check failure : 42 → 'f10:x'
</lang>
 
=={{header|Elena}}==
ELENA 4.x:
Line 897 ⟶ 904:
i = 10 j = 3 k = 4 j + 6 = 9 j + k = 7
</pre>
 
=={{header|Free Pascal}}==
''See [[#Pascal|Pascal]]''
<lang pascal>type
range = 1..10;
begin
range := 10;
{$rangeChecks on}
range := range + 10; // will yield a run-time error
end.;</lang>
The FPC (Free Pascal compiler) by default does ''not generate'' range checks.
Assigning a value out of range is possible.
Only constant expressions, i. e. expressions that can be evaluated entirely during compile-time, are not accepted, if they are out of range.
 
The compiler directive <tt>{$rangeChecks on}</tt> will enable insertion of range checks.
If an out of range assignment is attempted, a run-time error occurs.
 
Note, in <tt>{$mode Delphi}</tt> range checks are only switchable at procedural level, not on a per expression-basis.
 
=={{header|Go}}==
Line 1,681 ⟶ 1,706:
 
=={{header|Pascal}}==
<lang pascal>type
{{works with|Free_Pascal}}
naturalTen = 1..10;</lang>
The details of range checks are compiler specific. With Freepascal range checks can also be enabled on the command line with the -Cr option.
As per ISO 7185, any (attempted) assignment out of range constitutes an error.
<lang pascal>Program BoundInteger(output);
However, the processor (usually a compiler) is permitted to leave such an error undetected.
 
Modern compilers such as [[#Delphi|Delphi]] and [[#Free Pascal|Free Pascal]] give the programmer a choice, which behavior is desired, by providing compiler switches/directives.
{$RANGECHECKS ON}
 
type
TPartialInteger = 1..10;
 
var
testvar: TPartialInteger;
i: integer;
 
begin
for i := 1 to 11 do
begin
writeln(i);
testvar := i;
end;
end.</lang>
Output:
<pre>% ./BoundInteger
1
2
3
4
5
6
7
8
9
10
11
Runtime error 201 at $000113A6
$000113A6
$0002F586
$00011309
$00011238
$00000001
</pre>
 
=={{header|Perl}}==
149

edits