Eu improvements (part 4)
- Posted by Karl Bochert <kbochert at copper.net> Jan 04, 2007
- 755 views
Consider:
sequence wdlist -- a list of words -- -- Add a new word to a list of words and warn if some -- condition is met -- function add(sequence wordlist, sequence newword) integer warn append(wordlist, newword) ... -- check for warning prepend(wordlist, warn) return wordlist end function result = add(wordList, "new") wdlist = result[2] if result[1] == WARN then ...
(did I do that right??!?) . The function add() really should do what it says - add a word to the list. . Instead it must return a new list - Eu cannot modify its parameters. . The wdlist is copied several times -- it may be very large. . Returning a flag is awkward when the return value must be used for the list There are other ways to do this, but they all have serious flaws. It would be helpful if add() could be allowed to modify the list argument, but unrestrained pass-by-reference is very non-euphoric. Allow pass by reference only for the first argument of a function or procedure, and only if it is a sequence. This restricts PBR to only certain circumstances. When PBR is used, indicate it with a distinct syntax. Pull the first argument out of the parenthesis, and place it in front of the function name to indicate that it is a reference. (separate it from the functon name with a dot). Use the same syntax when defining the function.
sequence wdlist -- a list of words -- -- Add a new word to a list of words and warn if some -- condition is met -- function wdlist.add(sequence newword) --wdlist parameter must be a seq append(wdlist, newword) ... -- check for warning return warn end function if wdlist.add("new") == WARN then ...
Both the function and its use are cleaner and more understandable. The function add() really does add a word to the wdlist. Another way to understand 'wdlist.add()' is that it is saying "modify wdlist with the function add()" KtB