logoalt Hacker News

moomintoday at 9:13 PM3 repliesview on HN

AFAICT, this means you won’t be able to define Either<string, string>, which is definitely a thing you sometimes want to do.


Replies

sheepttoday at 9:23 PM

It seems like if you wrap both in a record then it should be possible:

    public record Left<T>(T Value);
    public record Right<T>(T Value);
    public union Either<L, R>(Left<L>, Right<R>);
jzebedeetoday at 9:22 PM

C# is strongly-typed, not stringly-typed. The point of the union is to list possible outcomes as defined through their respective types.

The idiomatic way to do this would be to parse, don't validate [1] each string into a relevant type with a record or record struct. If you just wanted to return two results of the same type, you'd wrap them in a named tuple or a record that represented the actual meaning.

[1] https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...

show 1 reply
throw1234567891today at 9:20 PM

but can you define T1 and T2 of string, then use Either<T1, T2>?

show 2 replies