Search code examples
language-ext

Language-Ext Unions - Target runtime doesn't support default interface implementation


I'm trying to user the Union type in Language-Ext and am getting error: "Target runtime doesn't support default interface implementation" for the method name and "The type or namespace name 'InconsistentFirstName' could not be found"

My code looks like this:

[Union]
public interface Inconsistency
{
    public Inconsistency InconsistentFirstName(string localValue, string contactCentreValue);
    public Inconsistency InconsistentSurname(string localValue, string contactCentreValue);

    public string GetFieldName(Inconsistency inconsistency)
        => inconsistency switch
        {
            InconsistentFirstName n => "Name",
            InconsistenctSurname s => "Surname",
            _ => ""
        };
}

Solution

  • GetFieldName needs to be added to a separate class file. There is a partial class that is automatically code generated called InconsistencyCon, so what I was trying to achieve can be done with the following code:

    
    [Union]
    public abstract partial class Inconsistency
    {
        public abstract Inconsistency InconsistentFirstName(string localValue, string remoteValue); 
        public abstract Inconsistency InconsistentSurname(string localValue, string remoteValue);
    }
    
    public static partial class InconsistencyCon
    {
         public static string FieldName(this Inconsistency inconsistency) =>
             inconsistency switch
             {
                 InconsistentFirstName n => "Name",
                 InconsistentSurname s   => "Surname",
                 _  => ""
             };
    
         public static string Test(Inconsistency inconsistency)
            {
                if (inconsistency is InconsistentFirstName n)
                {
                    var a = n.LocalValue;
                    var b = n.FieldName;
                    // etc
                }
            }
    }