Hello,
I would like to report the following bug : in the declaration of a
function, when an "Optional" parameter declares its type definition,
that type is not assigned to the parameter immediately, but only after
the first use of this parameter by the function.
Example :
1 Option Compatible: Option VBASupport 1
2 Function TestCompatible(*Optional xyz As Long = 5*)
3 If xyz = 5 Then
4 xyz = 23
5 else
6 print "KO"
7 end if
8 print xyz
9 End Function
This function should obviously print out '23', however it prints out "KO" !
Here is the explanation why :
- setting a break at line 3 shows that xyz has a value "Missing parameter"
- the condition of line 3 thus evaluates to False,
- and execution continues at line 6, printing "KO"
- note that breaking again at line 8 shows that xyz now has a type
'Variant/String' with value "5"
Discussion :
- Andrew already reported this bug in 2016 in its 'OOME Pitonyak 2016
_3_0 UserGuide.pdf', page 58.
- Note that without the "Option Compatible" the function could be
written also :
2 Function TestCompatible(Optional xyz) As Long
2.5 If IsMissing(xyz) then xyz = 5
and in this case there is no bug (and it is then normal for xyz to
have that value "Missing parameter", used by IsMissing).
-> Thus as a preliminary conclusion, it shows that the parser
actually processes the "As Long = 5", because the value "5" is displayed
at line 8,
but it does not apply this type and value immediately : it sets the
value to "Missing parameter" (just as if ignoring Option Compatible at
that moment)
To demonstrate that the type of xyz is assigned only after the first use
of this parameter, look at the following example now, where a line 2bis
has been inserted :
1 Option Compatible: Option VBASupport 1
2 Function TestCompatible(Optional xyz As Long = 5)
*2bis **xray xyz*
3 If xyz = 5 Then
4 xyz = 23
5 else
6 print "KO"
7 end if
8 print xyz
9 End Function
This function is the same as previous one and should also obviously
print out '23', however it now prints out '5' !!
Here is the explanation why :
- xray of xyz tells us that xyz is of type "String" with value "5" ;
this is confirmed by setting a break at line 3 which shows that xyz is
"Variant/String"
- the condition thus evaluates to True, as it compares xyz to Cstr("5")
- line 4 is thus executed, but ... *the assignment xyz = 23 is not
performed* !
Discussion :
- The same behavior will result if the line 2bis is replaced by any
statement making use of the variable xyz, like e.g. "txt$ = xyz"
- We observe that upon the first use of the parameter xyz in the
function, its value changes from "Missing parameter" to its declared value,
but without changing its type to its declared type.
Conclusion : there are in fact potentially 3 bugs to report :
1. the parameter xyz has a value "Missing parameter" when it is first
used :
with Option Compatible the parameter should receive the declared
type as soon as the function begins
2. after the first use of this parameter in a statement, it hasthe right
value but a type "Variant/String" instead of the declared type "Long" :
the declared type should be transferred over
3. it is not possible to assign any value to the parameter
Versions tested : OO Version 4.1.6
OO Version 4.1.13
notice: on LO Version 6.0.7.3, same behavior, except : the
assigmnent "xyz = 23" works (the value is assigned).
Thank you all !
Lucien.