Skip to content

IValueOf with static abstract From(TValue) at the cost of compatibility#20

Open
BreadTh wants to merge 1 commit intomcintyre321:masterfrom
BreadTh:master
Open

IValueOf with static abstract From(TValue) at the cost of compatibility#20
BreadTh wants to merge 1 commit intomcintyre321:masterfrom
BreadTh:master

Conversation

@BreadTh
Copy link
Copy Markdown

@BreadTh BreadTh commented Sep 17, 2021

Perhaps a change for later when there's greater compatibility, but having the static From function in an interface is amazing! (and just what I needed)

@mcintyre321
Copy link
Copy Markdown
Owner

What's an example of usage?

@BreadTh
Copy link
Copy Markdown
Author

BreadTh commented Sep 17, 2021

Allowing a class (StringExpectation) that is derrived from ValueOf to call the static .From on a class (GreetingMessage) that in turn derrives from itself. ValueOf => StringExpectation => GreetingMessage

Simplified example:

public class StringExpectation<TSelf> : ValueOf<string, TSelf> where TSelf : StringExpectation<TSelf>, IValueOf<string, TSelf>
{
    public static TSelf From(JToken token) =>
        //Do a bunch of validation with the token, and finally:
        TSelf.From(token.ToString());
}

public class GreetingMessage : StringExpectation<GreetingMessage> { }

The compiler does not understand that TSelf.From(string) exists alone from TSelf : StringExpectation<TSelf>, or even from TSelf : ValueOf<string, TSelf>

Implementing IValueOf<TValue, TThis> with static abstract From(TValue) allows such behaviour.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants