Special Type Challenge
Jonathan Marler via Digitalmars-d
digitalmars-d at puremagic.com
Fri Feb 6 17:55:25 PST 2015
I'm wondering if the following is possible in D. I tried and
failed but maybe someone else will be able to pull it off.
// Challenge: Create a type named "Byte" that,
// 1. Uses 1 byte of memory
// 2. Can be used as an argument to a non-template function
// 3. Handles implicit conversion from any 1-byte type
// 4. Compiles and passes the following unittest
unittest
{
Byte b;
b = 0;
b = 1;
b = 255;
b = -256;
b = 'a';
b = cast(const char)'a';
b = cast(immutable char)'a';
b = cast(byte)1;
b = cast(const byte)1;
b = cast(immutable byte)1;
b = cast(ubyte)1;
b = cast(const ubyte)1;
b = cast(immutable ubyte)1;
Byte echo(Byte b)
{
return b;
}
b = echo('a');
b = echo(cast(const char)'a');
b = echo(cast(immutable char)'a');
b = echo(cast(byte)1);
b = echo(cast(const byte)1);
b = echo(cast(immutable byte)1);
b = echo(cast(ubyte)1);
b = echo(cast(const ubyte)1);
b = echo(cast(immutable ubyte)1);
Byte[] barr;
barr = "teststring";
barr = [0,1,2,3];
}
More information about the Digitalmars-d
mailing list