Comparing slice to an Array in assert

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Tue Jun 11 09:24:26 UTC 2024


On 11/06/2024 9:17 PM, madwebness wrote:
> On Tuesday, 11 June 2024 at 08:44:25 UTC, Richard (Rikki) Andrew 
> Cattermole wrote:
>> There are two more corrections to make.
>>
>> Both in sliceIt.
>>
>> Note: array is a function not a type and is for ranges, slices are 
>> built in language concept. Associate Arrays (map) use the syntax 
>> ``Type[Type]``.
> 
> While I do understand what you're saying, I'm not sure I understand how 
> to fix the code.
> 
> 1. Changing function's argument type to `Array(string)` results in the 
> following error: `Error: function declaration without return type.`
> 
> 2. Adding function type `Array(string) sliceIt(...)` results in the 
> following error: `Error: function declaration without return type.`
> 
> 3. Finally, changing function's argument type to `string[]`, so function 
> definition looks like this:
> ```
> auto extract_arg_names(string[] def_words_arr){...};
> ```

That is correct yes.

> results in, what I presume, is a runtime error:
 >
> ```
> core.exception.AssertError at definition/sliceit.d(42): unittest failure
> ----------------
> ??:? _d_unittestp [0x357a31]
> ??:? void arg.__unittest_L39_C1() [0x302495]
> ??:? void arg.__modtest() [0x34ba78]
> ??:? int 
> core.runtime.runModuleUnitTests().__foreachbody6(object.ModuleInfo*) 
> [0x37316a]
> ??:? int object.ModuleInfo.opApply(scope int 
> delegate(object.ModuleInfo*)).__lambda2(immutable(object.ModuleInfo*)) 
> [0x34dde3]
> ??:? int rt.minfo.moduleinfos_apply(scope int 
> delegate(immutable(object.ModuleInfo*))).__foreachbody2(ref 
> rt.sections_elf_shared.DSO) [0x380257]
> ??:? int rt.sections_elf_shared.DSO.opApply(scope int delegate(ref 
> rt.sections_elf_shared.DSO)) [0x382dd1]
> ??:? int rt.minfo.moduleinfos_apply(scope int 
> delegate(immutable(object.ModuleInfo*))) [0x3801e5]
> ??:? int object.ModuleInfo.opApply(scope int 
> delegate(object.ModuleInfo*)) [0x34ddb5]
> ??:? runModuleUnitTests [0x372fa0]
> ??:? void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int 
> function(char[][])*).runAll() [0x372728]
> ??:? void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int 
> function(char[][])*).tryExec(scope void delegate()) [0x3726b5]
> ??:? _d_run_main2 [0x37261e]
> ??:? _d_run_main [0x3723bf]
> ??:? main [0x34ba8d]
> ??:? __libc_start1 [0x82289eaf9]
> /usr/src/lib/csu/amd64/crt1_s.S:83 _start [0x30213f]
> ??:? ??? [0xe83fb403007]
> 1/1 modules FAILED unittests
> ```
> 
> PS. Found more correction in the original code. The variable 
> `def_words_arr` should just be `words_arr`. Was not paying attention 
> when simplifying the code to post here.

Yes, that is the second one.

My version that runs:

```d
import std.stdio;
import std.regex;
import std.string;
import std.array;

auto sliceIt(string[] words_arr) {
    int i = 1;
    string word;
    while(i < words_arr.length) {
      word = words_arr[i];
      if(matchFirst(word, r"^[A-Z]+$")) { break; }
      i++;
    }
    return words_arr[1..i];
}

void main() {
    auto words = ["HELLO", "world", "hi", "ENDOFTHERUNWAY"];
    auto resulting_arr = sliceIt(words);
    assert(resulting_arr == ["world", "hi"]);
}
```


More information about the Digitalmars-d-learn mailing list