www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Method definition

reply Tim Gunesh <tim.moldazhan gmail.com> writes:
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
next sibling parent Paul Backus <snarwin gmail.com> writes:
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
prev sibling next sibling parent bauss <jj_1337 live.dk> writes:
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
prev sibling parent reply Tim Gunesh <tim.moldazhan gmail.com> writes:
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
parent reply Adam D Ruppe <destructionator gmail.com> writes:
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
parent Tim Gunesh <tim.moldazhan gmail.com> writes:
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:
 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.
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.
Jul 21