Skip to content

[Ideas] Legacy support or move the decorator to struct level. #2

@DerekGooding

Description

@DerekGooding

Legacy framework compatibility

What if instead, usage looked like this:

[Default]
public partial interface IDefaultInterface
{
    internal static void FooDefault() => Console.WriteLine("Default Foo implementation");
    internal readonly static int BarDefault = 42;
}

public partial struct MyStruct : IDefaultInterface;

And the generator created this:

public partial interface IDefaultInterface
{
    public void Foo();
    public int Bar { get; }
}

public readonly partial struct MyStruct()
{
    public readonly void Foo() => IDefaultInterface.FooDefault();
    public int Bar { get; } = IDefaultInterface.BarDefault;
}

Slightly Cleaner Usage

Alternatively, if you're going to rely on the .netCore default interface feature, consider moving the decorator attribute to the struct instead of the interface.

public interface IExample
{
    void Foo() => Console.WriteLine("Hello");
    int Bar => 123;
}

[ImplementForwardedDefaults]
public partial struct MyStruct : IExample;

Then verbatim the code implementation to the partial struct

public partial struct MyStruct
{
    public void Foo() => Console.WriteLine("Hello");
    public int Bar => 123;
}

Have the generator check if a method, field or property has a default implementation, then apply it if it does. It's cleaner to put the decorator on the 1 liner.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions