Define a primitive data type: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) 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 cast() = value; |
|||
constructor(int n) |
|||
{ |
|||
if (n <= 1 || n >= 10) |
|||
{ |
|||
InvalidArgumentException.raise() |
InvalidArgumentException.raise() |
||
}; |
|||
value := n |
|||
} |
|||
cast t(string s) |
|||
{ |
|||
value := s.toInt(); |
|||
if (value <= 1 || value >= 10) |
|||
{ |
|||
InvalidArgumentException.raise() |
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() |
public program() |
||
{ |
{ |
||
TinyInt i := 4t; |
|||
TinyInt j := i + i; |
|||
console.printLine("4t = ", i); |
|||
⚫ | |||
console.printLine("8t = ", j); |
|||
⚫ | |||
console.writeLine("4t + 8t = "); |
|||
⚫ | |||
⚫ | |||
i + j |
i + j |
||
} |
|||
catch(InvalidArgumentException e) |
|||
{ |
|||
console.printLine("A value is out of range") |
|||
} |
|||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
{{out}} |
|||
<pre> |
|||
default implementation |
|||
default implementation |
|||
delegate implementation |
|||
</pre> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |