Skip to content

Union typing is only needed when you have a statically typed language, as you need to declare that an object can return one of multiple types (in your case an int or string , or in the other example string or null)

License

ChengYen-Tang/Typing.Union

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Type.Union

NuGet
Union typing is only needed when you have a statically typed language, as you need to declare that an object can return one of multiple types (in your case an int or string , or in the other example string or null)

Example

using Typing;

namespace Example;
public class Program
{
    public static void Main(string[] args)
    {
        Print(5);
        Print("Hello");
        Print(new[] { "Hello", "World" });
        //Output:
        //5
        //Hello
        //Hello, World

        Console.WriteLine(GetStringLength(100));
        Console.WriteLine(GetStringLength("Hello"));
        Console.WriteLine(GetStringLength(new[] { "Hello", "World" }));
        //Output:
        //3
        //5
        //12
    }

    public static void Print(Union<int, string, string[]> printValue)
        => printValue.MatchAction(
            i => Console.WriteLine(i),
            s => Console.WriteLine(s),
            sa => Console.WriteLine(string.Join(", ", sa)));

    public static int GetStringLength(Union<int, string, string[]> value)
        => value.MatchFunc(
                i => i.ToString().Length,
                s => s.Length,
                sa => string.Join(", ", sa).Length);
}

About

Union typing is only needed when you have a statically typed language, as you need to declare that an object can return one of multiple types (in your case an int or string , or in the other example string or null)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages