Define a primitive data type: Difference between revisions

Content added Content deleted
(Added Java example)
m (Switch to header template)
Line 2: Line 2:
Demonstrate how to define a type that behaves like an integer but has a lowest valid value of 1 and a highest valid value of 10. Include all bounds checking you need to write, or explain how the compiler or interpreter creates those bounds checks for you.
Demonstrate how to define a type that behaves like an integer but has a lowest valid value of 1 and a highest valid value of 10. Include all bounds checking you need to write, or explain how the compiler or interpreter creates those bounds checks for you.


==[[Ada]]==
=={{header|Ada}}==
[[Category:Ada]]

type My_Type is range 1..10;
type My_Type is range 1..10;
The compiler identifies the range of valid values from the range specification ''1..10'' and automatically builds in bounds checking where it is needed. The compiler is smart enough to omit bounds checking when it is not needed.
The compiler identifies the range of valid values from the range specification ''1..10'' and automatically builds in bounds checking where it is needed. The compiler is smart enough to omit bounds checking when it is not needed.
Line 79: Line 77:
};
};


==[[E]]==
=={{header|E}}==
[[Category:E]]

def MyNumber := 1..10
def MyNumber := 1..10


Line 239: Line 235:
Similarly, we could declare the subtype of floating point numbers with restricted exponent, and so on.
Similarly, we could declare the subtype of floating point numbers with restricted exponent, and so on.


==[[Perl]]==
=={{header|Perl}}==
[[Category:Perl]]
'''Interpreter:''' [[Perl]] 5
'''Interpreter:''' [[Perl]] 5


Line 264: Line 259:
# dies, too small. string is 0 interpreted numerically
# dies, too small. string is 0 interpreted numerically


==[[Toka]]==
=={{header|Toka}}==
[[Category:Toka]]

needs quotes
needs quotes
{
{