Thread overview
Method definition
Jul 21, 2021
Tim Gunesh
Jul 21, 2021
Paul Backus
Jul 21, 2021
bauss
Jul 21, 2021
Tim Gunesh
Jul 21, 2021
Adam D Ruppe
Jul 21, 2021
Tim Gunesh
July 21, 2021

Is it possible to define methods outside the class in C ++ style? Something like this:

class Parent{
   void Print();
}

void Parent.Print(){
   writeln("Hello, D!");
}
July 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:

class Parent{
   void Print();
}

void Parent.Print(){
   writeln("Hello, D!");
}

No, it's not possible.

However, uniform function call syntax allows functions defined outside of a class to be called as though they were methods. For example:

class Parent {}

void Print(Parent p)
{
    writeln("Hello, D!");
}

void example()
{
    Parent p = new Parent;
    p.Print(); // passes `p` as first argument
}
July 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:

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.

July 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:

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!

July 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.

July 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:

>

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.