On Saturday, May 12, 2012 15:17:22 Manu wrote: > On 12 May 2012 14:03, Jonathan M Davis <jmdavisp...@gmx.com> wrote: > > On Saturday, May 12, 2012 13:39:54 Manu wrote: > > > Awesome, although it doesn't end there. Local references are also very > > > important. > > > > Never going to happen. Walter doesn't like them (they also cause issues > > with > > the proposal to let ref take rvalues in D IMHO), but I don't remember the > > details. Regardless, I'd be shocked if they were ever introduced into D. > > So what's the work around then? > Using pointers pollute your usage with pointer assignment problems, > and leads to many redundant instances of &, *, and parentheses all over the > place. > > Another scenario to consider: > given: ref X someFunc(); > > ref X t = someFunc(); // fail > > Without ref locals, there is no way to hold a local reference to something > returned by a function, which defeats the purpose... > And if ref is not a type constructor, auto and templates will never work > properly either.
I believe that you can do it with a pointer. auto t = &someFunc(); And as long as you don't try to increment the pointer, it's @safe. It doesn't even require different semantics when calling member functions, unlike C++ (though you'd still have to dereference it if you want to copy it, so the code would still differ somewhat from what you want in terms of syntax). Regardless, unfortunately, I don't remember why Walter doesn't want ref local variables. You'd have to get him to explain that. I just remember that he doesn't want them and I don't think that it wasn't really up for discussion. Maybe you can find an argument to convince him. I don't know. But from what I recall, there isn't much chance of that. > Well, while I'm sure that your use cases are very important and may be > > > normal > > in your industry, I don't think that they're normal in the programming > > world > > in general. > > I've never met a C++ programmer (other than yourself apparently) that > doesn't pass structs/classes by ref. > That's generally considered absurd. And if you want evidence, just look at > the STL, Boost, or any other C++ library you care to pick. C++ is not D. The semantics of passing arguments are not the same (thanks primary to postblit vs copy constructors). D creates fewer copies than C++. And actually, with C++'s move constructors, as I understand it, the advice is to no longer use const& for passing objects because it screws up the compiler's ability to optimize out temporaries (and the C++11 situation is much closer to D's situation than C++98 was). But regardless, I was never advocating that you _always_ pass structs by value. I was pointing out that for smaller structs (those with only a few members), it generally works just fine - except maybe in cases where you're worrying about absolutely every CPU cycle. - Jonathan M Davis