-
Notifications
You must be signed in to change notification settings - Fork 164
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
Unexpected behavior with interfaces #154
Comments
for interfaces you have to use OneOf<IFoo> test_fail = OneOf<IFoo>.FromT0(IFoo.AFoo); for concrete types you can implement custom implicit/explicit operators (like it is descripted in the readme readme) public partial class OneOfIFoo : OneOfBase<IFoo>
{
public OneOfIFoo(OneOf.OneOf<IFoo> _) : base(_) { }
public static implicit operator OneOfIFoo(IFoo _) => new OneOfIFoo(_); //CS0552 'OneOfIFoo.implicit operator OneOfIFoo(UserQuery.IFoo)': user-defined conversions to or from an interface are not allowed
public static explicit operator IFoo(OneOfIFoo _) => _.AsT0; //CS1503 Argument 1: cannot convert from 'IFoo' to 'OneOf.OneOf<IFoo>'
} won't compile :/ (more on this on stack overflow) |
I actually hate interfaces so I'm quite glad it's disallowed. 🙃
…On Tue, 11 Jul 2023, 21:31 romfir, ***@***.***> wrote:
for interfaces you have to use FromTX methods
OneOf<IFoo> test_fail = OneOf<IFoo>.FromT0(IFoo.AFoo);
for concrete types you can implement custom implicit/explicit operators
(like it is descripted in the readme readme
<https://github.com/mcintyre321/OneOf#reusable-oneof-types-using-oneofbase>
)
or use source generator do to it for you, but in C# it is not possible to
use implicit/explicit operators for interfaces so code like this
public partial class OneOfIFoo : OneOfBase<IFoo>{
public OneOfIFoo(OneOf.OneOf<IFoo> _) : base(_) { }
public static implicit operator OneOfIFoo(IFoo _) => new OneOfIFoo(_); //CS0552 'OneOfIFoo.implicit operator OneOfIFoo(UserQuery.IFoo)': user-defined conversions to or from an interface are not allowed
public static explicit operator IFoo(OneOfIFoo _) => _.AsT0; //CS1503 Argument 1: cannot convert from 'IFoo' to 'OneOf.OneOf<IFoo>'
}
won't compile :/ (more on this on stack overflow
<https://stackoverflow.com/questions/143485/implicit-operator-using-interfaces>
)
—
Reply to this email directly, view it on GitHub
<#154 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACDJ6UNIGWVG6LJJZDF7YLXPWZ2XANCNFSM6AAAAAA2DBT7VU>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm having the below error, any thoughts on this? Hopefully the answer is not "don't use interface types".
The text was updated successfully, but these errors were encountered: