digitalmars.D.learn - Function implemented outside the class
- MarisaLovesUsAll (12/12) Mar 23 2014 Hi! I didn't find how to implement functions outside the class,
- bearophile (5/12) Mar 23 2014 It's not possible. Some people want it to happen, some other
- John Colvin (33/45) Mar 23 2014 No, it's not possible.
- John Colvin (2/22) Mar 23 2014 sorry, 2 features.
- Matej Nanut (19/19) Mar 23 2014 Hello!
- MarisaLovesUsAll (14/38) Mar 24 2014 2 all:
- Steven Schveighoffer (10/50) Mar 24 2014 Implementation outside the class is not exactly possible, but it IS
- Rikki Cattermole (22/34) Mar 24 2014 There is one way that could be made far nicer with templates and
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
Mar 23 2014
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
Mar 23 2014
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, AlexeyNo, 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);
Mar 23 2014
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:sorry, 2 features.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, AlexeyNo, it's not possible. However, Combining these 3 features:
Mar 23 2014
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?
Mar 23 2014
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
Mar 24 2014
On Mon, 24 Mar 2014 16:02:25 -0400, MarisaLovesUsAll <marusya 2ch.hk> wrote:On Monday, 24 March 2014 at 01:34:22 UTC, Matej Nanut wrote: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. -SteveHello! 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.
Mar 24 2014
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, AlexeyThere 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).
Mar 24 2014