Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 08, 2018 sumtype 0.5.0 | ||||
---|---|---|---|---|
| ||||
SumType is a generic sum type for modern D. It is meant as an alternative to `std.variant.Algebraic`. Features: - Pattern matching, including support for structural matching (*) - Self-referential types, using `This` - Works with `pure`, `@safe`, `@nogc`, and `immutable` (*) - Zero runtime overhead compared to hand-written C - No heap allocation - Does not rely on runtime type information (`TypeInfo`) (*) Starred features (*) are those that are missing from `Algebraic`. Code examples are available in the documentation (linked below). New since the last announced version, 0.3.0: - Types with destructors and postblits are now handled correctly. - Unreachable handlers in `match` calls are now a compile-time error. - `match` handlers can now operate on the stored value by reference. - A new method, `tryMatch`, allows for non-exhaustive pattern matching. - Various small improvements to the documentation. Documentation: https://pbackus.github.io/sumtype/sumtype.html DUB: https://code.dlang.org/packages/sumtype Github: https://github.com/pbackus/sumtype |
August 08, 2018 Re: sumtype 0.5.0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Wednesday, 8 August 2018 at 20:54:13 UTC, Paul Backus wrote:
> SumType is a generic sum type for modern D. It is meant as an alternative to `std.variant.Algebraic`.
>
> Features:
> - Pattern matching, including support for structural matching (*)
> - Self-referential types, using `This`
> - Works with `pure`, `@safe`, `@nogc`, and `immutable` (*)
> - Zero runtime overhead compared to hand-written C
> - No heap allocation
> - Does not rely on runtime type information (`TypeInfo`) (*)
>
> Starred features (*) are those that are missing from `Algebraic`.
>
> Code examples are available in the documentation (linked below).
>
> New since the last announced version, 0.3.0:
> - Types with destructors and postblits are now handled correctly.
> - Unreachable handlers in `match` calls are now a compile-time error.
> - `match` handlers can now operate on the stored value by reference.
> - A new method, `tryMatch`, allows for non-exhaustive pattern matching.
> - Various small improvements to the documentation.
>
> Documentation: https://pbackus.github.io/sumtype/sumtype.html
> DUB: https://code.dlang.org/packages/sumtype
> Github: https://github.com/pbackus/sumtype
Nice,
but destructor SumType.~this() can call destroy on reference type like class:
bool destructed = false;
class C{
~this(){
destructed = true;
}
}
struct S{
~this(){
}
}
void main(){
auto c = new C;
{
auto st = SumType!(C, S)(c);
}
assert(destructed == true);
}
|
August 08, 2018 Re: sumtype 0.5.0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to vit | On Wednesday, 8 August 2018 at 21:57:07 UTC, vit wrote:
> Nice,
> but destructor SumType.~this() can call destroy on reference type like class:
Whoops. Good catch. I've pushed a fix, tagged as version 0.5.1.
|
August 09, 2018 Re: sumtype 0.5.0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Wednesday, 8 August 2018 at 20:54:13 UTC, Paul Backus wrote: > SumType is a generic sum type for modern D. It is meant as an alternative to `std.variant.Algebraic`. > > Features: > - Pattern matching, including support for structural matching (*) > - Self-referential types, using `This` > - Works with `pure`, `@safe`, `@nogc`, and `immutable` (*) > - Zero runtime overhead compared to hand-written C > - No heap allocation > - Does not rely on runtime type information (`TypeInfo`) (*) > > Starred features (*) are those that are missing from `Algebraic`. > > Code examples are available in the documentation (linked below). > > New since the last announced version, 0.3.0: > - Types with destructors and postblits are now handled correctly. > - Unreachable handlers in `match` calls are now a compile-time error. > - `match` handlers can now operate on the stored value by reference. > - A new method, `tryMatch`, allows for non-exhaustive pattern matching. > - Various small improvements to the documentation. > > Documentation: https://pbackus.github.io/sumtype/sumtype.html > DUB: https://code.dlang.org/packages/sumtype > Github: https://github.com/pbackus/sumtype That's a pretty cool library! I added it to run.dlang.io, s.t. it's easy for people to play with it and share examples, e.g. https://run.dlang.io/is/5znJXH (It uses the version from DUB and updates itself once daily, but somehow dub still lists 0.4.1 at the moment) |
August 09, 2018 Re: sumtype 0.5.0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seb | On Thursday, 9 August 2018 at 00:07:05 UTC, Seb wrote: > (It uses the version from DUB and updates itself once daily, but somehow dub still lists 0.4.1 at the moment) It looks like you didn't push the git tag to GitHub: https://github.com/pbackus/sumtype/releases |
August 09, 2018 Re: sumtype 0.5.0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seb | On Thursday, 9 August 2018 at 00:11:22 UTC, Seb wrote:
> On Thursday, 9 August 2018 at 00:07:05 UTC, Seb wrote:
>> (It uses the version from DUB and updates itself once daily, but somehow dub still lists 0.4.1 at the moment)
>
> It looks like you didn't push the git tag to GitHub:
>
> https://github.com/pbackus/sumtype/releases
One of these days, I'll learn. Should be there now.
|
August 09, 2018 Re: sumtype 0.5.0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Thursday, 9 August 2018 at 01:04:43 UTC, Paul Backus wrote:
> On Thursday, 9 August 2018 at 00:11:22 UTC, Seb wrote:
>> On Thursday, 9 August 2018 at 00:07:05 UTC, Seb wrote:
>>> (It uses the version from DUB and updates itself once daily, but somehow dub still lists 0.4.1 at the moment)
>>
>> It looks like you didn't push the git tag to GitHub:
>>
>> https://github.com/pbackus/sumtype/releases
>
> One of these days, I'll learn. Should be there now.
SumType without initialization doesn't fail if first type has @disabled this().
In SumType need be something like this:
struct SumType(...){
///...
static if(Types.length > 0 && __traits(compiles, (){Types[0] tmp;}()) == false){
///Types[0] has disabled this()
@disable this();
}
///....
}
method toString is not template. (why is there in the first place?)
SumType can have zero TypeArgs but then tryMatch fail:
auto st = SumType!();
st.tryMatch!((_) => 1); //fail at compile time with bad error message.
in matchImpl can by something like this:
static if(Types.length == 0){
static if(exhaustive) {
static assert(0, "No value to match");
} else {
throw new MatchException("No value to match");
}
}
else{
///...
}
or:
struct SumType(TypeArgs...)
if(TypeArgs.length > 0){
}
|
August 09, 2018 Re: sumtype 0.5.0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Thursday, 9 August 2018 at 01:04:43 UTC, Paul Backus wrote:
> ...
@safe opAssign can call @system postblit:
bool moved = false;
struct S{
this(this)@system{
moved = true;
}
}
void main()@safe{
auto st = SumType!(S).init;
st = S.init;
assert(moved == true);
}
change:
() @trusted { moveEmplace(rhs, storage.values[i]); }();
to:
moveEmplace(rhs, *(() @trusted => &storage.values[i])());
|
August 09, 2018 Re: sumtype 0.5.0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Thursday, 9 August 2018 at 01:04:43 UTC, Paul Backus wrote:
> On Thursday, 9 August 2018 at 00:11:22 UTC, Seb wrote:
>> On Thursday, 9 August 2018 at 00:07:05 UTC, Seb wrote:
>>> (It uses the version from DUB and updates itself once daily, but somehow dub still lists 0.4.1 at the moment)
>>
>> It looks like you didn't push the git tag to GitHub:
>>
>> https://github.com/pbackus/sumtype/releases
>
> One of these days, I'll learn. Should be there now.
ignore the message with opAssign, my version doesnt work :)
|
August 09, 2018 Re: sumtype 0.5.0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to vit | On Thursday, 9 August 2018 at 07:52:12 UTC, vit wrote: > SumType without initialization doesn't fail if first type has @disabled this(). It did fail for me, but it also failed *with* initialization too, so there was definitely a bug there. > method toString is not template. (why is there in the first place?) Mainly it's there so I can do `writeln(sumTypeInst);` without writing out the full `match` call every time. I can't see any reason why it should be a template. > SumType can have zero TypeArgs but then tryMatch fail: > > [...] > > struct SumType(TypeArgs...) > if(TypeArgs.length > 0){ > } The second option is what `Algebraic` does, and it seems like the most sensible solution to me. |
Copyright © 1999-2021 by the D Language Foundation