digitalmars.D.learn - Method definition
- Tim Gunesh (10/10) Jul 21 2021 Is it possible to define methods outside the class in C ++ style?
- Paul Backus (19/29) Jul 21 2021 No, it's not possible.
- bauss (6/16) Jul 21 2021 No and it doesn't make much sense to do so.
- Tim Gunesh (6/16) Jul 21 2021 I was afraid that this is not possible. I'm partly used to this
- Adam D Ruppe (7/9) Jul 21 2021 The D compiler can auto-generate listings like that. dmd -H makes
- Tim Gunesh (4/13) Jul 21 2021 Lots of thanks Adam! I'm new to D and this is the language I fell
Is it possible to define methods outside the class in C ++ style? Something like this: ```d class Parent{ void Print(); } void Parent.Print(){ writeln("Hello, D!"); } ```
Jul 21 2021
On Wednesday, 21 July 2021 at 12:08:21 UTC, Tim Gunesh wrote:Is it possible to define methods outside the class in C ++ style? Something like this: ```d class Parent{ void Print(); } void Parent.Print(){ writeln("Hello, D!"); } ```No, it's not possible. However, [uniform function call syntax][1] allows functions defined outside of a class to be called as though they were methods. For example: ```d class Parent {} void Print(Parent p) { writeln("Hello, D!"); } void example() { Parent p = new Parent; p.Print(); // passes `p` as first argument } ``` [1]: https://tour.dlang.org/tour/en/gems/uniform-function-call-syntax-ufcs
Jul 21 2021
On Wednesday, 21 July 2021 at 12:08:21 UTC, Tim Gunesh wrote:Is it possible to define methods outside the class in C ++ style? Something like this: ```d class Parent{ void Print(); } void Parent.Print(){ writeln("Hello, D!"); } ```No and it doesn't make much sense to do so. The reason why you have to do in C++ is because of header files and source files are separate. Since D uses modules then definition and implementation doesn't have to be separated.
Jul 21 2021
On Wednesday, 21 July 2021 at 12:08:21 UTC, Tim Gunesh wrote:Is it possible to define methods outside the class in C ++ style? Something like this: ```d class Parent{ void Print(); } void Parent.Print(){ writeln("Hello, D!"); } ```I was afraid that this is not possible. I'm partly used to this convenience in C++. This allows you to quickly understand the content of the class, especially in large undocumented projects. I'd love to move this to D. Thanks everyone for the reply and thanks to Paul Backus for an alternative way of defining methods!
Jul 21 2021
On Wednesday, 21 July 2021 at 13:56:11 UTC, Tim Gunesh wrote:This allows you to quickly understand the content of the class, especially in large undocumented projects.The D compiler can auto-generate listings like that. dmd -H makes a .di file with the bodies stripped out you can pursue, or a documentation generator like my adrdox can make HTML files with the method prototypes, even if there are no other docs (use --document-undocumented arg to adrdox for that). So you can accomplish these goals through other means.
Jul 21 2021
On Wednesday, 21 July 2021 at 14:02:42 UTC, Adam D Ruppe wrote:On Wednesday, 21 July 2021 at 13:56:11 UTC, Tim Gunesh wrote:Lots of thanks Adam! I'm new to D and this is the language I fell in love with the first time :). And tools like yours (https://github.com/adamdruppe/adrdox) are important to me.This allows you to quickly understand the content of the class, especially in large undocumented projects.The D compiler can auto-generate listings like that. dmd -H makes a .di file with the bodies stripped out you can pursue, or a documentation generator like my adrdox can make HTML files with the method prototypes, even if there are no other docs (use --document-undocumented arg to adrdox for that). So you can accomplish these goals through other means.
Jul 21 2021