Dealing with type information loss in Parameters Fields and Return type
Anonymouse via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Apr 17 05:15:19 PDT 2016
On Sunday, 17 April 2016 at 10:27:10 UTC, Nicholas Wilson wrote:
> On Sunday, 17 April 2016 at 10:22:00 UTC, Anonymouse wrote:
>> On Sunday, 17 April 2016 at 10:12:29 UTC, Nicholas Wilson
>> wrote:
>>> So currently there is a loss of information when Parameters
>>> Fields and Return type.
>>> i.e. assuming 64 bits
>>> size_t foo(ptrdiff_t) {};
>>>
>>> writeln(ReturnType!foo); // prints ulong
>>>
>>> Is there any way to get the types as (tuples of) strings of
>>> the the types as they are in the source file?
>>>
>>> auto foos = StringReturnType!foo;
>>> static assert(typeof(foos) == string);
>>> pramga(msg, foos); // available at CT, prints size_t
>>
>> Correct me if I'm wrong but size_t is just an alias of ulong
>> (assuming 64 bits), so they're the exact same thing. A rose by
>> any other name etc.
>
> not on 32bits its not. Also consider a Platform specific alias.
> The end result is that it is not portable.
"Assuming 64 bits". According to the spec as quoted, it will be
"a type that is large enough to represent an offset into all
addressible memory", versioned to fit the platform. So if
anything, it seems to me that it is there precisely in the spirit
of portability, aliasing an integer type selected to suit the
platform it was compiled for. Having it be different on 32 bits
is the point.
https://github.com/dlang/druntime/blob/master/src/object.d#L32
While string is likewise an alias to immutable(char)[], I think
it's just specialcased to be displayed as "string" anyway in the
majority of messages.
void main() {
string foo;
immutable(char)[] foo_alt;
size_t bar;
ptrdiff_t baz;
assert(typeid(foo).toString == "immutable(char)[]");
assert(typeof(foo).stringof == "string"); // <--
inconsistent
assert(typeid(foo_alt).toString == "immutable(char)[]");
assert(typeof(foo_alt).stringof == "string"); // <--
inconsistent-er
version (D_LP64) {
assert(typeid(bar).toString == "ulong");
assert(typeid(baz).toString == "long");
}
else {
assert(typeid(bar).toString == "uint");
assert(typeid(baz).toString == "int");
}
}
If you want size_t to be represented as a discrete type by that
StringReturnType, I imagine you will have to specialcase size_t
and ptrdiff_t in it to report them as "size_t" and "ptrdiff_t"
explicitly. Bear in mind that this will clobber ulong/uint and
long/int, because they really are the same.
More information about the Digitalmars-d-learn
mailing list