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

Unexpected behavior with interfaces #154

Open
NWoodsman opened this issue Jul 8, 2023 · 2 comments
Open

Unexpected behavior with interfaces #154

NWoodsman opened this issue Jul 8, 2023 · 2 comments

Comments

@NWoodsman
Copy link

I'm having the below error, any thoughts on this? Hopefully the answer is not "don't use interface types".

OneOf<IFoo> test_fail = IFoo.AFoo; // CS0029: Cannot implicitly convert type <IFoo> to OneOf<IFoo>
OneOf<IFoo> test_pass = new Foo();

internal interface IFoo
{
    internal static IFoo AFoo => new Foo();
}

internal class Foo : IFoo {}
@romfir
Copy link
Contributor

romfir commented Jul 11, 2023

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)
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)

@mcintyre321
Copy link
Owner

mcintyre321 commented Jul 11, 2023 via email

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

3 participants