Opaque type (struct) with a static immutable fails to compile without constructor - why?

frankp frankp_12983 at gmail.com
Thu Mar 4 14:42:16 UTC 2021


On Thursday, 4 March 2021 at 14:31:23 UTC, Simen Kjærås wrote:
> On Thursday, 4 March 2021 at 13:58:48 UTC, frankp wrote:
>> Hi all,
>>
>> I want to make an opaque type that simply contains an integer 
>> with some immutable constants and toString pretty printing. 
>> Like this:
>>
>> struct Foo_t
>> {
>>    private long foo;
>>    alias foo this;
>>    static immutable long Inf = long.max; //1)
>>
>>    void toString(...){}
>> }
>>
>> On dmd 2.092.1 this fails with:
>>   1) Error: cannot implicitly convert expression 9223...L of 
>> type immutable(long) to Foo_t
>>
>> I simply want to initialize an immutable long with long.max.
>> Why the conversion to Foo_t ?
>>
>> If I add a constructor:
>> private this(long f)
>> {
>>    foo = f;
>> }
>>
>> It compiles but according to code coverage this constructor is 
>> never called.
>> What's going on?
>
> I tried compiling your code locally on DMD 2.094.1, and had no 
> issues. Again with 2.095.0, no issues. On run.dlang.io, with 
> all dmd compilers from 2.060, and it just plain works.
>
> Now, that's after removing ... from toString. With that 
> present, it fails with some variation of 'Error: undefined 
> identifier '__va_list_tag''.
>
> Most likely, you have shortened your program for clarity, and 
> removed the issue you're experiencing in the process. Can we 
> have another one, with the issue still there?
>
> --
>   Simen

toString was actually added at a later point. The compiler 
complained prior to that.
So if I rename the struct to Foo_t in my program it compiles.

However, the compiler complains if the struct is called 
"cycle_t". That's curious. Is that a reserved name?
I don't import anything by that name. Not to my knowledge at 
least - I'm strictly using named imports.


More information about the Digitalmars-d-learn mailing list