trying to understand in, inout, and ref...

mark mark at qtrac.eu
Wed Jan 22 10:49:07 UTC 2020


I have these code snippets:

alias WordList = string[];
alias WordSet = int[string]; // key = word; value = 0

WordList generate(WordSet allWords, int steps) {
     WordList ladder; // will be changed in update()
     auto words = allWords.dup; // will be changed in update()
     auto compatibles = allWords.dup; // will be changed in this 
function
     auto prev = update(ladder, words, compatibles);
     // TODO
     return ladder;
}

string update(WordList ladder, WordSet words, WordSet 
compatibles) {
     auto word = ""; // TODO random string from compatibles
     // TODO remove word from words; add word to ladder
     return word;
}

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`?

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? compatibles is (will be) modified in 
generate() but not in update(), so should it be `in WordSet 
compatibles`?


More information about the Digitalmars-d-learn mailing list