Comments (5)
I really like this idea. The only small change I'd suggest is to use CaseOf
, rather than CaseOfType
, but if you are happy to code it and submit a PR, I'll go with your solution.
from succinct.
Ok, I'm working on it. A bit stuck at the moment, so I won't be offended if anyone else wants to implement it. But will be circling back to try to make it work shortly.
from succinct.
Quick sanity check here. I have tried adding CaseOf<T>
to Union<T1,T2>
. However, as it's not possible to use OR to constrain a generic type, I can't do something like:
public UnionPatternCaseHandler<UnionOfTwoPatternMatcher<T1, T2>, T> CaseOf<T>()
where T : T1|T2 ...
The best I can do is:
public UnionPatternCaseHandler<UnionOfTwoPatternMatcher<T1, T2>, T> CaseOf<T>()
{
if (typeof(T) == typeof(T1))
{
return new UnionPatternCaseHandler<UnionOfTwoPatternMatcher<T1, T2>, T1>(RecordAction, this)
as UnionPatternCaseHandler<UnionOfTwoPatternMatcher<T1, T2>, T>;
}
if (typeof(T) == typeof(T2))
{
return new UnionPatternCaseHandler<UnionOfTwoPatternMatcher<T1, T2>, T2>(RecordAction, this)
as UnionPatternCaseHandler<UnionOfTwoPatternMatcher<T1, T2>, T>;
}
throw new InvalidCaseOfTypeException(typeof(T));
}
This means the following would be supported:
public static string ElseExample(Union<int, bool> value) =>
value.Match<string>()
.CaseOf<int>().Of(0).Do("0")
.CaseOf<bool>().Of(false).Do("false")
.Else("??")
.Result();
and that'll work fine. However, eg CaseOf<string>()...
could be used in the above expression and cannot be checked for at compile-time. Instead, an InvalidCaseOfTypeException
would be thrown at runtime.
Does that found worth doing? If so, I'll add it as a new feature to the Union classes for the next release (likely 1-2 weeks away).
from succinct.
To me that definitely sounds worth doing. I tried something similar, but was stuck on the same issue. Many thanks! Sorry I wasn't able to contribute it myself.
from succinct.
This feature has been implemented in 1.6.0, which is now released.
from succinct.
Related Issues (20)
- Generic ValueOrError HOT 3
- A non-exception Value<T> alternative for unions HOT 2
- Maybe.Some(null) does not throw and has a different behavior than Maybe.None() HOT 8
- SuccincT Roadmap HOT 12
- When will version 3.1.1 be published to NuGet? HOT 3
- Remove Maybe type and change Option to be a readonly struct HOT 5
- Checklist for the v4 release HOT 1
- As C# 7 now supports discards, the Ignore function is redundant and should be removed HOT 1
- As C# 7 now supports discards, the Ignore function is redundant and should be removed HOT 1
- Add pattern matching to the Either type HOT 1
- Runtime Exception of type System.IO.FileNotFoundException HOT 4
- Change enum parsers to use Enum generic constraint HOT 1
- Add positional pattern matching support to Option<T> HOT 1
- Add positional pattern matching support to ValueOrError types HOT 1
- Add positional pattern matching support to Success<T> HOT 1
- Add positional pattern matching support to Either<TL,TR> HOT 1
- Add positional pattern matching support to Union types HOT 1
- Investigate adding cons support for async streams
- Remove the use of <T> at the end of Succinc<T>
- Create a new release of SuccincT, targeting .NET 6, with all the redundant features removed HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from succinct.