How to make a transparent wrapper type?

Random D user via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Dec 8 02:39:12 PST 2015


On Tuesday, 8 December 2015 at 10:26:18 UTC, Random D user wrote:
> On Monday, 7 December 2015 at 20:03:07 UTC, Namespace wrote:
>> This seems to work:
>> ----
>> struct RefVal(T) {
>> 	private T* ptr;
>> 	
>>     this(T* val) {
>> 		ptr = val;
>> 	}
>> 	
>>     ref auto opAssign(U)(auto ref U value) {
>> 		*ptr = value;
>> 		
>> 		return *ptr;
>> 	}
>> 	
>> 	auto get() inout {
>> 		return ptr;
>> 	}
>> }
>> ----
>
> Yes. It works for assignment as expected. Thanks. I don't know 
> why I didn't try that. I mean I tried something like this:
>
> struct RefVal(T)
> {
>
> }

Whoops. For some reason lost focus to window while typing and 
accidentally sent the message.

Well. Anyway, I tried something similar using alias this and 
template functions, but obviously it didn't work.

Unfortunately. Your version doesn't work with methods. For 
example if Ref!T is Ref!Struct then ref.method() doesn't work. 
That's the reason for alias this.
But it's good enough with public ptr.

Maybe opDispatch could help here. I haven't really used it so far.


More information about the Digitalmars-d-learn mailing list