On Wednesday, 25 June 2014 at 15:33:16 UTC, bearophile wrote:
In Phobos we have Typedef (that needs some improvements), that
allows to define a not compatible type.
In D we have "alias this" that inside a struct allows to create
a new type (with instance object of size equal or larger than
the original type) with similar semantics of a given type. This
allows to define a type that acts "like" another.
In Ada there is a standard way to restrict a type. In a Reddit
thread I have found this simple example of modern Ada code, a
sufficiently common need:
My go-to example is social-security numbers, because it's
simple
enough that people can see what's going on as well as provides
a
good way to illustrate how subtypes can allow one to 'ignore'1
the implementation.
-- SSN format: ###-##-####
Subtype Social_Security_Number is String(1..11)
with Dynamic_Predicate =>
(for all Index in Social_Security_Number'Range =>
(case Index is
when 4|7 => Social_Security_Number(Index) = '-',
when others => Social_Security_Number(Index) in '0'..'9'
)
);
-- The above declaration can allows me to skip the validity
-- checks of parameters within the body of a subprogram as
-- the constraints are checked on subprogram-call.
-- I do not need to check the validity of the return value,
-- an attempt to return a non-conformant string will raise
-- and exception.
Function Get_SSN( Record : ID ) return Social_Security_Number;
-- Likewise, passing a non-conformant value to SSN will raise
-- an exception.
Procedure Save_SSN( Record : ID; SSN : Social_Security_Number
);
I think where you expect a String you can send a
Social_Security_Number, but where it expects a
Social_Security_Number it doesn't accept a String. (The
Dynamic_Predicate and Static_Predicate of modern Ada allow to
perform such restrictions.) So I think there are some analogies
with OOP.
I'd like to do something like this in D, with structs/values.
Bye,
bearophile
You can do this in C# as well:
class SocialSecurityNumber
{
string _ssn;
//Implicitly converts to string
public static implicit operator string(SocialSecurityNumber
ssn)
{
return _ssn;
}
}