Thread overview | |||||
---|---|---|---|---|---|
|
November 10, 2020 Disallow implicit "conversion" from alias-types | ||||
---|---|---|---|---|
| ||||
Hello. I am unsure if I am going about this the right way, and if my question even makes sense. In essence what I want is to have two "types" represented by a size_t. Here is an example of what I want think I want (but might be completely off) alias Foo = size_t; alias Bar = size_t; Foo foo = 4; Bar bar = foo; // i want some error like // "cannot implicitly convert from type Foo to Bar" My best solution is to have the types as classes to force type checking. Is there a better way to do what I want here? |
November 10, 2020 Re: Disallow implicit "conversion" from alias-types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimirs Nordholm | On Tuesday, 10 November 2020 at 11:38:30 UTC, Vladimirs Nordholm wrote: > Hello. > > I am unsure if I am going about this the right way, and if my question even makes sense. > > In essence what I want is to have two "types" represented by a size_t. Here is an example of what I want think I want (but might be completely off) > > alias Foo = size_t; > alias Bar = size_t; > > Foo foo = 4; > Bar bar = foo; // i want some error like > // "cannot implicitly convert from type Foo to Bar" > > My best solution is to have the types as classes to force type checking. > > Is there a better way to do what I want here? https://dlang.org/library/std/typecons/typedef.html |
November 10, 2020 Re: Disallow implicit "conversion" from alias-types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry | On Tuesday, 10 November 2020 at 11:49:19 UTC, Jerry wrote:
> On Tuesday, 10 November 2020 at 11:38:30 UTC, Vladimirs Nordholm wrote:
>> Hello.
>>
>> I am unsure if I am going about this the right way, and if my question even makes sense.
>>
>> In essence what I want is to have two "types" represented by a size_t. Here is an example of what I want think I want (but might be completely off)
>>
>> alias Foo = size_t;
>> alias Bar = size_t;
>>
>> Foo foo = 4;
>> Bar bar = foo; // i want some error like
>> // "cannot implicitly convert from type Foo to Bar"
>>
>> My best solution is to have the types as classes to force type checking.
>>
>> Is there a better way to do what I want here?
>
> https://dlang.org/library/std/typecons/typedef.html
This is exactly what I need. Thanks!
|
Copyright © 1999-2021 by the D Language Foundation