Re: Function implemented outside the class
On Sunday, 23 March 2014 at 21:48:33 UTC, MarisaLovesUsAll wrote: Hi! I didn't find how to implement functions outside the class, like in C++. C++: class MyClass { void myFunc(); }; void MyClass::myFunc() { ... } Is it possible to do this in D ? Sorry for my bad English. Regards, Alexey There is one way that could be made far nicer with templates and traits but.. import std.stdio; alias void delegate() testfunc; class T { this() { test = () { test_(this); }; } testfunc test; } void test_(T t) { writeln(hi from test); } void main() { T t = new T(); t.test(); } There will be other ways to make this nicer. If you feel you really really want this sort of thing (advised not to).
Re: Function implemented outside the class
On Monday, 24 March 2014 at 01:34:22 UTC, Matej Nanut wrote: Hello! You can implement static functions that act like members, like so: --- void myFunc(MyClass c) { ... } --- Which you will be able to call like: --- auto c = new MyClass(); c.myFunc(); --- because of uniform function call syntax (UFCS). But they won't be real methods (virtual member functions), which means they can't be overridden. Note that you can use the class's private members in such functions, because private things in D are private to the file (module) instead of the containing class or struct. I don't think it's possible to do the same thing as in C++ though; but I might be wrong. 2 all: Thanks for replies! Why would you like to do that? I planned to use it to take event handling out from class (and put it in another file), but now I see that isn't a good idea. class App { void updateEvents(SDL_Event event) { ... } } By the way, it would be useful if it was written somewhere that implementation outside the class is impossible. Sorry for bad English. Regards, Alexey
Re: Function implemented outside the class
On Mon, 24 Mar 2014 16:02:25 -0400, MarisaLovesUsAll maru...@2ch.hk wrote: On Monday, 24 March 2014 at 01:34:22 UTC, Matej Nanut wrote: Hello! You can implement static functions that act like members, like so: --- void myFunc(MyClass c) { ... } --- Which you will be able to call like: --- auto c = new MyClass(); c.myFunc(); --- because of uniform function call syntax (UFCS). But they won't be real methods (virtual member functions), which means they can't be overridden. Note that you can use the class's private members in such functions, because private things in D are private to the file (module) instead of the containing class or struct. I don't think it's possible to do the same thing as in C++ though; but I might be wrong. 2 all: Thanks for replies! Why would you like to do that? I planned to use it to take event handling out from class (and put it in another file), but now I see that isn't a good idea. class App { void updateEvents(SDL_Event event) { ... } } By the way, it would be useful if it was written somewhere that implementation outside the class is impossible. Implementation outside the class is not exactly possible, but it IS possible to separate declaration from implementation, see D interface (.di) files. You can't split an implementation into two files, however. Note, there are serious drawbacks for using an interface file, most importantly eliminating the ability to inline. It should only be used, IMO, when you need to hide the implementation, as in a closed-source project. -Steve
Function implemented outside the class
Hi! I didn't find how to implement functions outside the class, like in C++. C++: class MyClass { void myFunc(); }; void MyClass::myFunc() { ... } Is it possible to do this in D ? Sorry for my bad English. Regards, Alexey
Re: Function implemented outside the class
MarisaLovesUsAll: C++: class MyClass { void myFunc(); }; void MyClass::myFunc() { ... } Is it possible to do this in D ? It's not possible. Some people want it to happen, some other people hate it. Bye, bearophile
Re: Function implemented outside the class
On Sunday, 23 March 2014 at 21:48:33 UTC, MarisaLovesUsAll wrote: Hi! I didn't find how to implement functions outside the class, like in C++. C++: class MyClass { void myFunc(); }; void MyClass::myFunc() { ... } Is it possible to do this in D ? Sorry for my bad English. Regards, Alexey No, it's not possible. However, Combining these 3 features: UFCS (universal function call syntax, foo(x) can be rewritten as x.foo() ) private means private to the module, not private to the class //one.d module one; class A { private int i; } int getI(A c) { return c.i; } //two.d module two; void main() { auto a = new A; auto i = A.getI(); assert(i == int.init); } Note that: getI is just a normal function. It can still be called like getI(a);. This means they by definition aren't virtual and cannot override inherited methods. the same syntax works for anything, not just classes. E.g. core.time.msecs can be called like this: auto t = 42.msecs(); instead of this: auto t = msecs(42);
Re: Function implemented outside the class
On Sunday, 23 March 2014 at 22:20:39 UTC, John Colvin wrote: On Sunday, 23 March 2014 at 21:48:33 UTC, MarisaLovesUsAll wrote: Hi! I didn't find how to implement functions outside the class, like in C++. C++: class MyClass { void myFunc(); }; void MyClass::myFunc() { ... } Is it possible to do this in D ? Sorry for my bad English. Regards, Alexey No, it's not possible. However, Combining these 3 features: sorry, 2 features.
Re: Function implemented outside the class
Hello! You can implement static functions that act like members, like so: --- void myFunc(MyClass c) { ... } --- Which you will be able to call like: --- auto c = new MyClass(); c.myFunc(); --- because of uniform function call syntax (UFCS). But they won't be real methods (virtual member functions), which means they can't be overridden. Note that you can use the class's private members in such functions, because private things in D are private to the file (module) instead of the containing class or struct. I don't think it's possible to do the same thing as in C++ though; but I might be wrong. Why would you like to do that?