Re: Complicated Types: Prefer “alias this” Over “alias” For Easier-To-Read Error Messages

Steven Schveighoffer schveiguy at yahoo.com
Mon May 21 21:12:31 UTC 2018


On 5/21/18 4:29 PM, Nick Sabalausky (Abscissa) wrote:
> On 05/21/2018 01:30 PM, Paul Backus wrote:
>>
>> I'm not sure making `_data` private is really a good idea. For 
>> example, this only works if `_data` is public:
>>
>> import std.algorithm;
>> import std.range;
>> import std.stdio;
>>
>> struct MyType
>> {
>>      auto _data = iota(10);
>>      alias _data this;
>> }
>>
>> void main()
>> {
>>      MyType x;
>>      x.each!writeln;
>> }
> 
> Ouch, and the error message isn't very helpful either. I wonder what 
> causes this to fail though, and whether it might simply be a compiler bug?

No, an alias to private data doesn't magically make it public.

The reason it doesn't work is that each is in std.algorithm.iteration, 
which has no visibility into private symbols of your main module.

This also fails:

assert(isInputRange!MyType);

-Steve


More information about the Digitalmars-d-announce mailing list