Skip to content
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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

BreadTh
Copy link

@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
Owner

What's an example of usage?

@BreadTh
Copy link
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