trying to understand in, inout, and ref...
mark
mark at qtrac.eu
Wed Jan 22 15:26:06 UTC 2020
On Wednesday, 22 January 2020 at 14:23:53 UTC, Paul Backus wrote:
> On Wednesday, 22 January 2020 at 10:49:07 UTC, mark wrote:
>> Regarding generate(): allWords should never be changed
>> (generate is called in a loop with the same allWords every
>> time) -- so should it be `in WordSet allWords`?
>
> For parameters that shouldn't be changed, use const. So it
> should be `const WordSet allWords`.
That single change produces (using LDC 1.19.0 - D 2.089.1):
./wordladder.d(52): Error: function wordladder.update(string[]
ladder, int[string] words, const(int[string]) compatibles) is not
callable using argument types (string[], const(int)[string],
const(int)[string])
./wordladder.d(52): cannot pass argument words of type
const(int)[string] to parameter int[string] words
Failed: ["/home/mark/opt/ldc2-1.19.0-linux-x86_64/bin/ldmd2",
"-v", "-o-", "./wordladder.d", "-I."]
So I've rolled it back.
>> Regarding update(): ladder and words are both modified in
>> update -- so should they be `ref WordList ladder` and `ref
>> WordSet words`? And if so, do I need to change the update()
>> call in the generate() function?
>
> Yes, they should be `ref`. No, you do not have to change the
> update() call.
I've done this but my impression from the docs is that passing
slices and associative arrays is already done by reference so
these aren't needed? (I can't tell yet because I haven't written
the modifying code.)
>> compatibles is (will be) modified in generate() but not in
>> update(), so should it be `in WordSet compatibles`?
>
> It should be `const WordSet compatibles`.
Done that and it compiles fine.
Thanks.
More information about the Digitalmars-d-learn
mailing list