Define a primitive data type: Difference between revisions

Content added Content deleted
m (syntaxhighlight doesn't have a default lang, need to provide one)
imported>Arakov
Line 664: Line 664:
sealed struct TinyInt : BaseNumber
sealed struct TinyInt : BaseNumber
{
{
int value;
int value;
int cast() = value;
int cast() = value;
constructor(int n)
constructor(int n)
{
{
if (n <= 1 || n >= 10)
if (n <= 1 || n >= 10)
{
{
InvalidArgumentException.raise()
InvalidArgumentException.raise()
};
};
value := n
value := n
}
}
cast t(string s)
cast t(string s)
{
{
value := s.toInt();
value := s.toInt();
if (value <= 1 || value >= 10)
if (value <= 1 || value >= 10)
{
{
InvalidArgumentException.raise()
InvalidArgumentException.raise()
}
}
}
}
TinyInt add(TinyInt t)
TinyInt add(TinyInt t)
= value + (cast int(t));
= value + (cast int(t));
TinyInt subtract(TinyInt t)
TinyInt subtract(TinyInt t)
= value - (cast int(t));
= value - (cast int(t));
TinyInt multiply(TinyInt t)
TinyInt multiply(TinyInt t)
= value * (cast int(t));
= value * (cast int(t));
TinyInt divide(TinyInt t)
TinyInt divide(TinyInt t)
= value / (cast int(t));
= value / (cast int(t));
bool equal(TinyInt t)
bool equal(TinyInt t)
= value == (cast int(t));
= value == (cast int(t));
bool less(TinyInt t)
bool less(TinyInt t)
= value == (cast int(t));
= value == (cast int(t));

string toPrintable()
=> value;
}
}
public program()
public program()
{
{
TinyInt i := 4t;
TinyInt i := 4t;
TinyInt j := i + i;
TinyInt j := i + i;
console.printLine("4t = ", i);
try
console.printLine("8t = ", j);
{
console.writeLine("4t + 8t = ");

try
{
i + j
i + j
}
}
catch(InvalidArgumentException e)
catch(InvalidArgumentException e)
{
{
console.printLine("A value is out of range")
console.printLine("A value is out of range")
}
}
}</syntaxhighlight>
}</syntaxhighlight>
{{out}}
<pre>
default implementation
default implementation
delegate implementation
</pre>


=={{header|Euphoria}}==
=={{header|Euphoria}}==