www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Ref!(T)? All we need now is opImplicitCast

reply "Janice Caron" <caron800 googlemail.com> writes:
std.slist implements a singly linked list, SList(T). The function
front() returns a Ref!(T).

Naturally, I jumped up and down with glee for a few moments, thinking
I'd uncovered undocumented references. But alas no. Ref!(T) is simply
aliased to T.

Still ... could be someone's thinking ahead?

It occurs to me that it's almost possible to implement a reference
type for real. If Ref!(T) were a struct which contained a T*, and
which implemented opAssign, opAddAssign, and all the other op*Assigns,
and if the implementation of those operators assigned through the
pointer, then it would work as an lvalue.

The problem is, though it wouldn't work as an rvalue ... yet. What it
would need for that to work is opImplicitCast, so that it could
auto-convert to a T by dereferencing the pointer.

Just a thought.
Dec 09 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Janice Caron wrote:
 std.slist implements a singly linked list, SList(T). The function
 front() returns a Ref!(T).
 
 Naturally, I jumped up and down with glee for a few moments, thinking
 I'd uncovered undocumented references. But alas no. Ref!(T) is simply
 aliased to T.
 
 Still ... could be someone's thinking ahead?
 
 It occurs to me that it's almost possible to implement a reference
 type for real. If Ref!(T) were a struct which contained a T*, and
 which implemented opAssign, opAddAssign, and all the other op*Assigns,
 and if the implementation of those operators assigned through the
 pointer, then it would work as an lvalue.
 
 The problem is, though it wouldn't work as an rvalue ... yet. What it
 would need for that to work is opImplicitCast, so that it could
 auto-convert to a T by dereferencing the pointer.
 
 Just a thought.

But still that would only work for something like Container!(int) and not Container!(MyStruct). Because you wouldn't be able to call MyStruct.member without creating all the necessary forwarding functions. Or any other op***s it has. So it's not just opAssign variants you need. Maybe alias this will give us what we need though, but I'm not so sure "alias this" on a pointer was in the original proposal. --bb
Dec 09 2007
parent BCS <BCS pathlink.com> writes:
Bill Baxter wrote:
 Janice Caron wrote:
 
 std.slist implements a singly linked list, SList(T). The function
 front() returns a Ref!(T).

 Naturally, I jumped up and down with glee for a few moments, thinking
 I'd uncovered undocumented references. But alas no. Ref!(T) is simply
 aliased to T.

 Still ... could be someone's thinking ahead?

 It occurs to me that it's almost possible to implement a reference
 type for real. If Ref!(T) were a struct which contained a T*, and
 which implemented opAssign, opAddAssign, and all the other op*Assigns,
 and if the implementation of those operators assigned through the
 pointer, then it would work as an lvalue.

 The problem is, though it wouldn't work as an rvalue ... yet. What it
 would need for that to work is opImplicitCast, so that it could
 auto-convert to a T by dereferencing the pointer.

 Just a thought.

But still that would only work for something like Container!(int) and not Container!(MyStruct). Because you wouldn't be able to call MyStruct.member without creating all the necessary forwarding functions. Or any other op***s it has. So it's not just opAssign variants you need. Maybe alias this will give us what we need though, but I'm not so sure "alias this" on a pointer was in the original proposal. --bb

the Ref(T) type could wrap all the opX's in: "static if(is(typeof(T.opX))" or something like that with traits or what not.
Dec 10 2007