Define a primitive data type: Difference between revisions

m
m (syntaxhighlight doesn't have a default lang, need to provide one)
imported>Arakov
Line 664:
sealed struct TinyInt : BaseNumber
{
int value;
int cast() = value;
constructor(int n)
{
if (n <= 1 || n >= 10)
{
InvalidArgumentException.raise()
};
value := n
}
cast t(string s)
{
value := s.toInt();
if (value <= 1 || value >= 10)
{
InvalidArgumentException.raise()
}
}
TinyInt add(TinyInt t)
= value + (cast int(t));
TinyInt subtract(TinyInt t)
= value - (cast int(t));
TinyInt multiply(TinyInt t)
= value * (cast int(t));
TinyInt divide(TinyInt t)
= value / (cast int(t));
bool equal(TinyInt t)
= value == (cast int(t));
bool less(TinyInt t)
= value == (cast int(t));
 
string toPrintable()
=> value;
}
public program()
{
TinyInt i := 4t;
TinyInt j := i + i;
console.printLine("4t = ", i);
try
console.printLine("8t = ", j);
{
console.writeLine("4t + 8t = ");
 
try
{
i + j
}
catch(InvalidArgumentException e)
{
console.printLine("A value is out of range")
}
}</syntaxhighlight>
{{out}}
<pre>
default implementation
default implementation
delegate implementation
</pre>
 
=={{header|Euphoria}}==
Anonymous user