On Thu, Aug 20, 2009 at 7:22 AM, Peter Alexander<peter.alexander...@gmail.com> wrote: > Lars T. Kyllingstad Wrote: > >> Disclaimer: You didn't say whether you use D1 or D2, but I use D2, so >> I'll give my answer in D2 code. It is highly likely it will also work in D1. > > I'm using neither :) I'm just considering learning at the moment. > > >> First of all, I don't know how it is in C++, but in D you rarely write >> function declarations without definitions. So unless you have a very >> general function body for your "general case", I'd simply drop it. If >> you have, the general case looks like this: >> >> <snip> >> >> For more info, check out http://www.digitalmars.com/d/2.0/template.html >> >> -Lars > > Excellent! Thanks a lot. I was hoping that D could overcome this problem. > > > Ah, one (maybe) final question: > > Is there an equivalent to friends in D (didn't see any in the docs)? If so, > do they work with templates easily? > > In my style of programming, I very rarely use member functions (I think they > are an atrocity in language design), so I create global functions for almost > everything, and when they need to access to private members, I have the class > declare the function as a friend. > > Does D support my style of programming?
Instead of friend, in D everything within one file (==one module) has access to everything else in that same file/module. So you can use your style, as long as you put the global functions in the same module as the classes operated on. > Here's a concrete example of something I'd like to do (pseudocode): > > class Foo { private int x; friend globalFun; } > class Bar { private int y; friend globalFun; } > > void globalFun(ref Foo foo, ref Bar bar) { foo.x = bar.y; } > > Is there anything like this in D? Export sounds like the right thing, but can > that be used in the example above, assuming that Foo and Bar are in separate > modules? I don't think export has anything to do with it. --bb