This will not even raise a warning:
xquery version "3.1";
let $a := 1
let $a := 2
return $a
(: yields 2 in all runtimes I tested :)
Why would a (functional) programming language allow variable re-declarations?
I really just want to get the rationale behind it.
By rejecting this at the compile step would enable some more aggressive optimisations, I believe.
Since local variables are also known to child scopes this just leaves more room for hard to catch errors.
JavaScript had this for years with var
and finally got rid of it by introducing let
and const
.
Here is an example where it might be hard to track values.
xquery version "3.1";
let $f := function ($c) {
(: some code ... :)
let $b := $a
let $a := 2
let $c := $b
(: some more code ... :)
return ($a, $b, $c)
}
let $a := 1
return ($a, $f($a), $a)
You might want to guess first, before evaluating it.