Skip to content

default interface methods #601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
eshadboltadr opened this issue Mar 18, 2025 · 2 comments
Open

default interface methods #601

eshadboltadr opened this issue Mar 18, 2025 · 2 comments

Comments

@eshadboltadr
Copy link

eshadboltadr commented Mar 18, 2025

Is your feature request related to a problem? Please describe.
I have an interface with a default interface method which is a property with a getter body defined in the interface definition.

I construct a concrete class derived from the interface and I pass it to the handlebars template as the context parameter (data parameter is null). The type of the context parameter is System.Object.

Handlebars doesn't seem to evaluate the default interface method, but it does for properties defined in the derived class.

Describe the solution you'd like
Can a new option be added to the handlebars environment configuration, so that default interface methods are included?

Describe alternatives you've considered
I don't want to write an explicit implementation of each property in each derived class because it wouldn't be DRY.

Is your feature request supported by Handlebarsjs? Please provide details.
N/A

Additional context

Template:

{{DateTimeStr}}
{{OtherStr}}

C#:

public interface IBaseData
{
    DateTime DateTimeUtc { get; }
}

public interface IData : IBaseData
{
    string DateTimeStr => DateTimeUtc.ToLocalTime().ToString("g");
}

public class Data : IData
{
    public DateTime DateTimeUtc { get; set; }
    public string OtherStr => DateTimeUtc.ToLocalTime().ToString("g");
}

string output = template(new Data { DateTimeUtc = DateTime.UtcNow })
@eshadboltadr eshadboltadr changed the title Substitute mustache with value from a default interface method default interface methods Mar 18, 2025
@eshadboltadr
Copy link
Author

Sounds like it might be possible if the template takes an IData, but it doesn't work if it takes a Data or IBaseData or System.Object.

@eshadboltadr
Copy link
Author

But probably not possible because IHandlebars.Compile returns HandlebarsTemplate<object, object> and we cannot specify the generic type argument for TContext.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant