Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Recursive variadic param bug


  • Please log in to reply
2 replies to this topic
infogulch
  • Moderators
  • 717 posts
  • Last active: Jul 31 2014 08:27 PM
  • Joined: 27 Mar 2008
These calls are equivalent:
test(  [1,2,3]   )
test([ [1,2,3] ]*) ; this is the same as above it just wraps it as an array and unpacks it again

test( v ) {
    msgbox % v.maxindex()
}

But when recursively calling a variadic param it fails:
msgbox % "final result:`n" objnests(test(1,[1,2,3]))

test( i, v* ) {
    msgbox % "nest #" i "`n" objnests(v)
    if i < 3
        return test(i+1, [v]*)  ; this works fine
        ;return test(i+1, v)    ; but this fails to re-wrap v as an array, and instead passes [""]
                                ; however they should be equivalent
    return v
}

objnests( obj ) {
    ; returns the maxindex of  obj and obj[1] recursively if obj[1] is an object
    ;     outermost maxindex on the left, innermost on the right
    ; e.g. 
    ;   [1,2,3]      returns: 3
    ;   [[1,2,3]]    returns: 1 <- 3
    ;   [[[1,2,3]]]  returns: 1 <- 1 <- 3 . in other words: this object has a maxindex of 1 with a child that has a maxindex of 1 and a grandchild that has a maxindex of 3
    return obj.maxindex() (IsObject(obj[1]) ? " <- " objnests(obj[1]) : "")
}


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
When a function is called recursively, its local variables are "backed up" (to be later restored) and cleared for the new call. Variables are passed by reference up until the value is copied into the target parameter, so in order to pass the value of a local variable recursively to the function which contains it, each variable reference in the parameter list needs to be "dereferenced" before the local variables are reset. This wasn't done for the "excess" parameters, since (before the introduction of variadic functions) they were always discarded. The fix is simple and will be included in the next release.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
This should not be a problem with v1.1.04.