Specify rhs at initialisation or assignment of typedef' d variable

Cecil Ward via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 31 01:53:10 PDT 2017


On Monday, 31 July 2017 at 07:50:57 UTC, inevzxui wrote:
> On Monday, 31 July 2017 at 07:16:25 UTC, Cecil Ward wrote:
>> Say I have used Typedef! to create some new type and I declare 
>> a variable, constant or enum of that type. Is there a way that 
>> I can express a literal value on the rhs without having to use 
>> casts, as that seems to defeat the point of the nice type 
>> safety?
>>
>> I may be asking for the impossible or _illogical_ here. In any 
>> case, I still get to keep the nice feature of not being able 
>> to mix up types with assignment from one variable to another.
>>
>> Specific example is
>>
>>     mac_addr_48_t   my_mac_address = 0x112233445566uL;
>>
>> Which now produces a compile time error after I changed to use 
>> an alias  = Typedef!uint64_t as opposed to just a straight 
>> alias = uint64_t earlier with no strong typing.
>
> If struct + alias this is not strong enough the only solution 
> is see is a helper template à la "octal" or "hexString", i.e a 
> static cally checked string.

I suspect that I am asking for something that literally makes no 
sense at all. I wanted to try and avoid opening the door to 
allowing the following kind of typing error now, eg
     enum ip_address = 0x11223344;
     mac_addr_48_t my_mac = cast(mac_addr_48_t) ip_address;
as if we are going to the bother of introducing strong type 
checking with Typedef! then the last thing I want to do is 
encourage is a proliferation of casts.

I realise something else now too -

Issue 2: The thing is that I also immediately have to do a lot of 
work to make the simplest operators work anyway, such as in

     foreach( addr;  base_mac_address .. base_mac_address + range )

where the + operator is producing compile-time errors now.

So it just seems that the Typedef! feature immediately make life 
into a nightmare. I don't know if something based of the physical 
units module (using 'dimensionless' in this case) would work - 
perhaps it only handles floating point of various types? Or 
whether that would also involve a huge amount of work and still 
have issue 1 mentioned earlier. In any case, I have absolutely no 
clue how to even begin to start using the units module thing.


More information about the Digitalmars-d-learn mailing list