My thoughts & tries with rvalue references
Zach the Mystic
reachzach at gggggmail.com
Fri Mar 29 22:32:38 PDT 2013
On Friday, 29 March 2013 at 13:45:48 UTC, Namespace wrote:
> Hey guys,
> I would like to show you my tries and thoughts about rvalue
> references and their syntax.
> As discussed in my other thread in the learn group
> (http://forum.dlang.org/thread/uswucstsooghescofycp@forum.dlang.org?page=2#post-mailman.294.1364252397.4724.digitalmars-d-learn:40puremagic.com),
> auto ref is not longer an option for non-template functions.
> Because of that I learned to read the compiler code and start
> my own tries, as you can read in my thread also
> (http://forum.dlang.org/thread/uswucstsooghescofycp@forum.dlang.org?page=3#post-jrksuqqremsqgicmybri:40forum.dlang.org).
> One of my tries was a kind of pseudo-property "@ref" and
> finally a hybrid of D and C++: ref&.
> Because I think a property isn't the best solution, I like to
> hear your thougths about my idea with "ref&".
> Here a small example:
>
> void bar1(ref& A a) { } // OK
> void bar2(ref &A a) { } // OK
> void bar21(&A a) { } // Error: '&' can only be used in
> combination with 'ref'.
> void bar22(& A a) { } // Error: '&' can only be used in
> combination with 'ref'.
> void bar3(ref const& A a) { } // OK
> void bar4(ref const &A a) { } // OK
> void bar5(ref &const A a) { } // Error: '&' Must be directly
> used in front of the type.
> void bar6(ref& const A a) { } // Error: '&' Must be directly
> used in front of the type.
>
> And for templates:
>
> void foo(T)(ref &T t) { }
>
> As you can see, '&' can only be used in front of the type and
> cannot exist without 'ref'.
>
> The advantages of this syntax would be:
> - it is short
> - It is easy to implement
> - it is known from C++
> - It does not cause conflicts with other things
> and it is (IMO) a very nice hybrid of C++ ref and D ref.
>
> Hope you like it, just like me. But I would like to hear your
> general opinions about it.
> Thanks in advance.
The major downside is that if you don't come from C++ it's hard
to understand why 'ref &' means what you propose. The major
upsides are, as you mention, it's very concise and perfectly
intuitive if you DO come from C++. In the spirit of trying to
come up with something for comparison, the best attribute I've
thought of so far is '@val':
void bar1(@val ref A a) { }
The advantage is that it's consistent with my understanding of
the general approach to adding things to D at this point. But
that's also it's disadvantage: it's nothing more than a mundane
attribute.
More information about the Digitalmars-d
mailing list