On Monday, 21 November 2016 at 20:04:51 UTC, Ali Çehreli wrote:
First, a reminder that we have this great resource of D idioms:
https://p0nce.github.io/d-idioms/#Rvalue-references:-Understanding-auto-ref-and-then-not-using-it
The link above has an idiom of mixing in a byRef() member
function to a struct. I think I've simplified the template by
moving typeof(this) inside it:
mixin template RvalueRef() // <-- DOES NOT TAKE A PARAMETER
ANY MORE
{
alias T = typeof(this);
static assert (is(T == struct));
@nogc @safe
ref const(T) byRef() const pure nothrow return
{
return this;
}
}
struct Vector2f
{
float x, y;
this(float x, float y) pure nothrow
{
this.x = x;
this.y = y;
}
mixin RvalueRef; // <-- SIMPLER USE
}
void foo(ref const Vector2f pos)
{
writefln("(%.2f|%.2f)", pos.x, pos.y);
}
void main()
{
Vector2f v = Vector2f(42, 23);
foo(v); // Works
foo(Vector2f(42, 23).byRef); // Works as well, and use
the same function
}
Let me know if it's not the equivalent of the original.
Ali
Sorry, but D seems to be worse and worse day by day.
This should be resolved by language and not doing it by template
function.
Same thing should be applied for maybe monad and tuples.
e.g. When I want to create simple function for drawing
void lineTo(auto ref Point point...) {
//...
}
It's easier to call it like:
Point p = Point(42, 42);
lineTo(p);
lineTo(42, 42);
lineTo(Point(42, 42)); // this
lineTo(Point(42, 42).byRef); // and this is just overhead
Everything possible should be solved by syntactic sugar rather
than implementing it as template func.
Or just remove shared, TLS and other stuff from D and implement
it as templates like in C++. Then you can write much more longer
and uglier stuffs as you trying.
void lineTo(std::shared_ptr<Point> point) {
// ...
}
lineTo(std::make<Point>(42, 42));
should be ideal way how to complicate programming for other users.
Sorry, but I want to write fast and safe programs in the fastest
possible way and writing Point(...) or Point(...).byRef every
time is redundant overhead.
Like http://pix.toile-libre.org/upload/original/1479816672.png
PS: sorry for sarcasm
- Satoshi