Thread overview
Comparing slices with std.variant.Algebraic
Sep 05, 2022
anonymouse
Sep 05, 2022
Ali Çehreli
Sep 05, 2022
anonymouse
Sep 05, 2022
Paul Backus
Sep 07, 2022
anonymouse
September 05, 2022

Observe the implementation of

stuct Axis(U...){}

More specifically, observe its usage in the unittests for Binary Ops on Variant Axis and Binary Ops on Variant + Other DataType

Note that both tests fail due to asserts on lines 422 and 452. Note also that commenting out these two lines results in successful compilation of all other tests. Inspecting c.data, one will find that it holds the array [1.7, 3.7, 5.7, 7.7, 9.7] in both cases, which is what is being asserted by those two lines.

So the question is, what is the proper way to compare a slice (array literal?) and an Algebraic in current D? I assume that this code worked back in 2019, however, I am unable to detect when it stopped working because no DMD compiler prior to v2.100.0 works properly on my system.

On a related note, std.variant.Algebraic has been deprecated and the suggested replacement is std.sumtype.SumType. What is the proper way to make this conversion? Attempting to do a drop-in replacement results in the following errors:

axis.d(400): Error: incompatible types for array comparison: `SumType!(bool, int, long, float, double, string, DateTime)[]` and `double[]`
axis.d(86): Error: incompatible types for `(this.data[i]) + (rhs.data[i])`: both operands are of type `SumType!(bool, int, long, float, double, string, DateTime)`
axis.d(414): Error: template instance `axis.Axis!void.Axis.opBinary!("+", void)` error instantiating
axis.d(86): Error: incompatible types for `(this.data[i]) + (rhs.data[i])`: `SumType!(bool, int, long, float, double, string, DateTime)` and `int`
axis.d(445): Error: template instance `axis.Axis!void.Axis.opBinary!("+", int[])` error instantiating
axis.d(43): Error: none of the overloads of template `object.get` are callable using argument types `!(int)(const(SumType!(bool, int, long, float, double, string, DateTime)))`
/Users/anonymouse/dlang/dmd-2.100.0/osx/bin/../../src/druntime/import/object.d(3409):        Candidates are: `get(K, V)(inout(V[K]) aa, K key, lazy inout(V) defaultValue)`
/Users/anonymouse/dlang/dmd-2.100.0/osx/bin/../../src/druntime/import/object.d(3416):                        `get(K, V)(inout(V[K])* aa, K key, lazy inout(V) defaultValue)`
axis.d(47): Error: none of the overloads of template `object.get` are callable using argument types `!(double)(const(SumType!(bool, int, long, float, double, string, DateTime)))`
/Users/anonymouse/dlang/dmd-2.100.0/osx/bin/../../src/druntime/import/object.d(3409):        Candidates are: `get(K, V)(inout(V[K]) aa, K key, lazy inout(V) defaultValue)`
/Users/anonymouse/dlang/dmd-2.100.0/osx/bin/../../src/druntime/import/object.d(3416):                        `get(K, V)(inout(V[K])* aa, K key, lazy inout(V) defaultValue)`
axis.d(474): Error: template instance `axis.Axis!void.Axis.convertTo!(int[])` error instantiating
axis.d(43): Error: none of the overloads of template `object.get` are callable using argument types `!(double)(const(SumType!(bool, int, long, float, double, string, DateTime)))`
/Users/anonymouse/dlang/dmd-2.100.0/osx/bin/../../src/druntime/import/object.d(3409):        Candidates are: `get(K, V)(inout(V[K]) aa, K key, lazy inout(V) defaultValue)`
/Users/anonymouse/dlang/dmd-2.100.0/osx/bin/../../src/druntime/import/object.d(3416):                        `get(K, V)(inout(V[K])* aa, K key, lazy inout(V) defaultValue)`
axis.d(47): Error: none of the overloads of template `object.get` are callable using argument types `!(double)(const(SumType!(bool, int, long, float, double, string, DateTime)))`
/Users/anonymouse/dlang/dmd-2.100.0/osx/bin/../../src/druntime/import/object.d(3409):        Candidates are: `get(K, V)(inout(V[K]) aa, K key, lazy inout(V) defaultValue)`
/Users/anonymouse/dlang/dmd-2.100.0/osx/bin/../../src/druntime/import/object.d(3416):                        `get(K, V)(inout(V[K])* aa, K key, lazy inout(V) defaultValue)`
axis.d(478): Error: template instance `axis.Axis!void.Axis.convertTo!(double[])` error instantiating

Thanks,
--anonymouse

September 05, 2022
On 9/5/22 01:58, anonymouse wrote:

> array [1.7, 3.7, 5.7, 7.7, 9.7] in both cases, which is what is being
> asserted by those two lines.

None of those values can be represented precisely in a floating point type. Without looking at the code, I wonder whether the tests will pass if you can manage to use the following values instead, which can be represented precisely:

  [1.5, 3.5, 5.5, 7.5, 9.5]

Ali

September 05, 2022
On Monday, 5 September 2022 at 10:30:32 UTC, Ali Çehreli wrote:
> On 9/5/22 01:58, anonymouse wrote:
>
> > array [1.7, 3.7, 5.7, 7.7, 9.7] in both cases, which is what
> is being
> > asserted by those two lines.
>
> None of those values can be represented precisely in a floating point type. Without looking at the code, I wonder whether the tests will pass if you can manage to use the following values instead, which can be represented precisely:
>
>   [1.5, 3.5, 5.5, 7.5, 9.5]
>
> Ali

It will not.

--anonymouse
September 05, 2022

On Monday, 5 September 2022 at 08:58:21 UTC, anonymouse wrote:

>

On a related note, std.variant.Algebraic has been deprecated and the suggested replacement is std.sumtype.SumType. What is the proper way to make this conversion? Attempting to do a drop-in replacement results in the following errors:

axis.d(400): Error: incompatible types for array comparison: `SumType!(bool, int, long, float, double, string, DateTime)[]` and `double[]`
axis.d(86): Error: incompatible types for `(this.data[i]) + (rhs.data[i])`: both operands are of type `SumType!(bool, int, long, float, double, string, DateTime)`
axis.d(414): Error: template instance ```

SumType does not attempt to forward operators to the contained value like Algebraic does, so you will have to use tryMatch to access the value(s) in cases like these.

// Compare a DataType[] to a double[]

import std.algorithm.comparison: equal;

/+
Will throw an exception if lhs contains a value that can't
be compared to a double.
+/
alias cmp = (DataType lhs, double rhs) => lhs.tryMatch!(value => value == rhs);

DataType[] a;
double[] b;
bool result = a.equal!cmp(b);
// Add two DataType values

/+
addValues will take two DataTypes as arguments and match on
both simultaneously.

If the two DataTypes do not contain values that can be added
together, an exception will be thrown.

See:
- https://dlang.org/phobos/std_sumtype.html#multiple-dispatch
- https://dlang.org/phobos/std_sumtype.html#introspection-based-matching
+/
alias addValues = tryMatch!((lhs, rhs) => lhs + rhs);

DataType a;
DataType b;
DataType result = addValue(a, b);
September 07, 2022

Thanks Paul. Gotta wrap my head around this well enough to update that module. However, this is a great start. Thank you very much.

--anonymouse