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

[Proposal] where expression #3701

Closed
fawdlstty opened this issue Jul 17, 2020 · 5 comments
Closed

[Proposal] where expression #3701

fawdlstty opened this issue Jul 17, 2020 · 5 comments

Comments

@fawdlstty
Copy link

It is recommended to add a WHERE expression. The purpose of the new expression is to allow an expression in the constraint operator to be evaluated as a value.Like the usual operator (+-*/%)

public class Foo {
    public static T add<T> (T a, T b) where [T + T] {
        return a + b;
    }
}
public class Foo1 {
    public static T opera<T> (T a, T b) where [T + T], [T - T], [T * T, [T / T], [T % T] {
        return a + b;
    }
}



Foo.add (1, 2); // compile success
Foo.add (1.2, 2.6); // compile success
Foo.add (new object (), new object ()); // compile failure

public class Bar {
    public int val = 5;
    public static Bar operator + (Bar a, Bar b) {
        return new Bar { val = a.val + b.val };
    }
}
Foo.add (new Bar (), new Bar ()); // compile success

public class Bar1 {
    public int val = 5;
    public static Bar1 operator - (Bar1 a, Bar1 b) {
        return new Bar1 { val = a.val - b.val };
    }
}
Foo1.opera (new Bar1 (), new Bar1 ()); // compile success
Foo.add (new Bar1 (), new Bar1 ()); // compile failure

Existing problem: There is no constraint that can directly make T type do mathematical operation, so it must be cast to dynamic, but cast or method directly accept dynamic type, which may make the type that cannot do mathematical operation can also be passed as a parameter, unable to check the code statically

the proposal can fix this problem

@yaakov-h
Copy link
Member

Seems like something better solved by Shapes (#164).

@juliusfriedman
Copy link

juliusfriedman commented Jul 17, 2020

How about a little something different here cause I sorta like this...

public class Foo {
    public static T add<T> (T a, T b) where T : [virtual] operator(+, -, *, /, %)
        return a + b;
    }
}
public class Foo1 {
    public static T opera<T> (T a, T b) where  [virtual] operator(+, -, *, /, %)
        return a + b;
    }
}

(the virtual can be committed and indicated derives is allowed, in or out would probably be used for that.)

public class Foo {
    public static T add<out T> (T a, T b) where T :  operator(+, -, *, /, %)
        return a + b;
    }
}
public class Foo1 {
    public static T opera<in T> (T a, T b) where T : operator(+, -, *, /, %)
        return a + b;
    }
}

Beats something liek this maybe:

Number

@juliusfriedman
Copy link

juliusfriedman commented Jul 17, 2020

Seems like something better solved by Shapes (#164).

Or Roles right? Definitely note Records :P

@fawdlstty
Copy link
Author

fawdlstty commented Jul 17, 2020

where T : [virtual] operator(+, -, *, /, %)

@juliusfriedman how to recognition i++ or ++i

@333fred
Copy link
Member

333fred commented Jul 17, 2020

#1711 is the current issue for static interface members and roles, which is what we're looking at for this space. This issue itself is mostly a duplicate of #3333, so I'm going to close it out and direct discussion to one of those two.

@333fred 333fred closed this as completed Jul 17, 2020
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

No branches or pull requests

4 participants