On Saturday, 2 April 2022 at 14:49:15 UTC, Vijay Nayar wrote:
On Saturday, 2 April 2022 at 14:35:10 UTC, Vijay Nayar wrote:
The `tryMatch` method fails to compile, and instead I get the following error:
```d
/dlang/dmd/linux/bin64/../../src/phobos/std/sumtype.d(2004): Error: static assert: "`handlers[0]` of type `int function(ref ubyte[] _1, ref ubyte[] _2) pure nothrow @nogc @safe` never matches"
```

Through sheer trial and error, I discovered that changing the handler arguments from `ref ubyte[]` to `const ref ubyte[]` the error goes away. However, I did not find any information in the documentation or code that made this requirement clear. It was basically a guess based on the fact that `string` has no trouble but `ubyte[]` did, and string is basically just `immutable(char[])`.

The reason `const` is required is that the arguments to your function, `v1` and `v2`, are `const`. In D, the `const` qualifier is transitive, so if you have a `const VarType`, whatever value it contains will also be qualified with `const`.

So far, I'm finding that learning to use `SumType` is significantly more cryptic than `Variant`, by a large margin. I'll still give it a shot of course, because I want to get past this problem.

It's possible to get more informative error messages using the `-verrors=spec` compiler flag, although it's a bit of a hassle. The basic process is

1. Recompile the module that the error occurs in with `-verrors=spec` and redirect output to a file.

    ```
    dmd -verrors=spec -c source/mymodule.d >verrors.txt 2>&1
    ```

2. Use `grep` or your text editor's search function to find errors that occurred in that source file or in `sumtype.d`.

    ```
    grep -e 'source/mymodule.d' -e 'std/sumtype.d' verrors.txt
    ```

Following these steps on your example code yields around 20 lines of output, which includes the "real" error message:

```
(spec:1) /usr/include/dmd/phobos/std/sumtype.d(1768): Error: function literal `__lambda3(ref ubyte[] _1, ref ubyte[] _2)` is not callable using argument types `(const(ubyte[]), const(ubyte[]))` (spec:1) /usr/include/dmd/phobos/std/sumtype.d(1768): cannot pass argument `_param_0` of type `const(ubyte[])` to parameter `ref ubyte[] _1`
```

Reply via email to