Skip to content

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

Open
BreadTh wants to merge 1 commit into
mcintyre321:masterfrom
BreadTh:master
Open

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

Conversation

@BreadTh

@BreadTh BreadTh commented Sep 17, 2021

Copy link
Copy Markdown

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

BreadTh commented Sep 17, 2021

Copy link
Copy Markdown
Author

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