Thread overview
Does anyone think C#'s partial would have any use in D?
Dec 19, 2021
Dr Machine Code
Dec 19, 2021
Rumbu
Dec 19, 2021
Dukc
December 19, 2021

From official C# docs, it can be useful such as:

  • When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.
  • When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.
  • When using source generators to generate additional functionality in a class.
    To split a class definition, use the partial keyword modifier, as shown here:

source: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods

Does anyone thinks C# would benefit of having this keyword? I think it was mostly implemented to make it easier the Visual Studio's code generator. Can't think much of it. But give D doesn't have even multiples inheritances, it's unlikely to be appreciated but just like to know you all opinions

December 19, 2021

On Sunday, 19 December 2021 at 04:24:30 UTC, Dr Machine Code wrote:

>

Does anyone thinks C# would benefit of having this keyword? I think it was mostly implemented to make it easier the Visual Studio's code generator. Can't think much of it. But give D doesn't have even multiples inheritances, it's unlikely to be appreciated but just like to know you all opinions

I suppose you wanted to say that D will benefit from it.

It was already discussed in the past, you can search the forum for it. The conclusion was that metaprogramming capabilities of D are enough to not mandate the use of partial classes.

partial class C
{
  //generated code
}

class C
{
  //custom code
}

can be achieved in D using mixins:

class C
{
  mixin!C //generated code
  //custom code
}

As a concrete example, you can browse the libdparse source code on github, especially ast.d code where the content of AST nodes is mostly generated by mixins.

An external tool like VS should generate the mixin content instead of class content.


Now back to the principle, partial classes will not work in D, because you cannot place a class in two different modules, their fully qualified name will be different.

December 19, 2021
On Sunday, 19 December 2021 at 06:30:08 UTC, Rumbu wrote:
>
> I suppose you wanted to say that D will benefit from it.
>
> It was already discussed in the past, you can search the forum for it. The conclusion was that metaprogramming capabilities of D are enough to not mandate the use of partial classes.

This, plus we have less need for that concept than C# does, because we can place functions on module scope. In C# classes are often used as namespaces for functions that are not conceptually member functions of anything, and in that case a partial class can make sense.

In D, if you have such a large class that it wants more than one file, that is a code smell. Move some of the (non-overridable) member functions out of it, or split it to smaller classes and/or structs.