Comments (8)
No, get
will never be public.
If you want something similar, I suggest a tryGet
function like the following:
T tryGet(T, ST)(ST st)
if (isSumType!ST)
{
return st.tryMatch!((T value) => value);
}
This will throw an exception when the requested type is not found.
from sumtype.
Yes, however that doesn't work with betterC
. If not get
, then maybe a @system
version of it. But if it isn't in your plans, which is understandable, then I'll just keep using match
.
That being said, I don't think a function to extract the value directly would damage the lib in any way, if it were @system
and well documented. It would come down to the user's choice if he wanted to use the function or not.
from sumtype.
Good API design is as much about what you leave out as about what you include. I want sumtype
to guide users into the pit of success, and providing a function that crashes at runtime if you use it incorrectly is contrary to that goal.
from sumtype.
How about adding a version of tryMatch
/tryGet
suitable for betterC
. This would assert
if the requested type wasn't found.
from sumtype.
You can write it yourself if you really want it. I will not include such a function in the sumtype
library, for the reason stated above.
from sumtype.
Hold my beer while I fork the standard library for my project... because I want to use something in betterC...?
from sumtype.
I don't know if this covers all cases, but based on your example above:
T tryGet(T, ST)(ST st)
if (isSumType!ST)
{
static if (st.Types.length == 1)
return st.match!((T value) => value);
else
return st.match!(
(T value) => value,
function T (_) { assert(false); }
);
}
Not really what I want, but it might do as a workaround.
from sumtype.
Personally I would recommend something like this for BetterC (and in general, honestly):
Nullable!T get(T, ST)(ST st)
if (isSumType!ST)
{
return st.match!(
(value) {
static if (is(typeof(value) == T))
return nullable(value);
else
return Nullable!T();
}
);
}
from sumtype.
Related Issues (20)
- Member type with copy-constructor fails to compile HOT 6
- Match does not play nice with assert(0) HOT 1
- Error message on no match type HOT 3
- Construction and assignment from lvalue require exact type match HOT 4
- Sum types cannot contain function pointers that reference 'This' by value HOT 1
- Confusing error message for type mismatch in `opEquals`
- Provide convenient access to the type index
- typeIndex gives inconsistent results for differently-qualified versions of same object
- Add a serialization/deserialization example to the documentation
- SumType fails to replace This[] inside Tuple!() HOT 1
- Confusing error message when matching immutable or const SumType HOT 1
- [REG 1.0.1] match no longer returns an lvalue
- Some assignments should be able to execute in safe code HOT 1
- Can't store scope pointers in a SumType HOT 4
- SumType doesn't hold a struct that defines a copy-constructor HOT 8
- Support `void` as a type in SumType HOT 1
- match has poor compile-time performance HOT 1
- Bad error message when having a single handler that's supposed to match everything HOT 1
- Can't construct inout SumType 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 sumtype.