Define a primitive data type: Difference between revisions

Content added Content deleted
Line 472: Line 472:
</lang>
</lang>
=={{header|Elena}}==
=={{header|Elena}}==
ELENA 3.4:
ELENA 4.x:
<lang elena>import extensions.
<lang elena>import extensions;

sealed struct TinyInt :: BaseNumber
sealed struct TinyInt : BaseNumber
{
{
int value.
int value;

cast T<int> = value.
int cast() = value;

constructor(int n)
constructor(int n)
[
{
if ((n <= 1)||(n >= 10))
if (n <= 1 || n >= 10)
[
{
InvalidArgumentException new; raise
InvalidArgumentException.raise()
].
};
value := n.
value := n
]
}

cast t(literal s)
[
value := s toInt.
cast t(string s)
if ((value <= 1)||(value >= 10))
[
{
InvalidArgumentException new; raise
value := s.toInt();
]
]

T<TinyInt> add(TinyInt t)
= value + T<int>(t).
if (value <= 1 || value >= 10)
T<TinyInt> subtract(TinyInt t)
= value - T<int>(t).
{
InvalidArgumentException.raise()
}
}
TinyInt add(TinyInt t)
= value + (cast int(t));
T<TinyInt> multiply(TinyInt t)
TinyInt subtract(TinyInt t)
= value * T<int>(t).
= value - (cast int(t));
T<TinyInt> divide(TinyInt t)
TinyInt multiply(TinyInt t)
= value / T<int>(t).
= value * (cast int(t));
TinyInt divide(TinyInt t)
= value / (cast int(t));
bool equal(TinyInt t)
bool equal(TinyInt t)
= value == T<int>(t).
= value == (cast int(t));
bool less(TinyInt t)
bool less(TinyInt t)
= value == T<int>(t).
= value == (cast int(t));
}
}

public program
[
T<TinyInt> i := 4t.
T<TinyInt> j := i + i.
public program()
try(i + j)
{
TinyInt i := 4t;
TinyInt j := i + i;
try
{
i + j
}
catch(InvalidArgumentException e)
{
{
console.printLine("A value is out of range")
on(InvalidArgumentException e)
[
}
}</lang>
console printLine("A value is out of range").
]
}.
]</lang>


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