When I declare a default value for an Optional
argument, but specify a later argument by name, the previous argument does not receive the specified default value. It looks as though it receives instead the default value for variables of the specified type.
Is this behaviour intentional? documented? To me personally it seems to be in obvious conflict with this statement in the help:
= expression: Specify a default value for the argument, matching its declared type. Optional is necessary for each argument specifying a default value.
It seems pretty embarrassing, as fixing this could break existing macros, but it has tripped me up many a time already. I could not find it in Bugzilla, searching LibreOffice/BASIC for “default optional argument”. At a minimum, if it cannot be fixed cleanly, the Help quoted above should accurately describe the actual behaviour.
In the following code, the first argument receives the value False
although the default was declared to be True
:
REM ***** BASIC *****
Option Explicit
Option Compatible
' Demonstrate a bug in assigning values to optional parameters:
' When an optional parameter is specified by name,
' preceding unspecified optional parameters do not honour default values specified in the declaration.
Sub Bug_Optional_Default_Ignored ()
Bug_Optional_Default_Ignored_Sub (Y_B := True)
End Sub
' Demonstrate a bug in assigning values to optional parameters, as above
Sub Bug_Optional_Default_Ignored_Sub ( Optional y_A as Boolean = True _
, Optional y_B as Boolean = False _
)
MsgBox ("A = " + y_A + ", B = " + y_B)
End Sub
I believe I have seen this in a number of versions, but currently I am seeing it in this:
Version: 7.6.4.1 (X86_64) / LibreOffice Community
Build ID: e19e193f88cd6c0525a17fb7a176ed8e6a3e2aa1
CPU threads: 8; OS: Linux 5.3; UI render: default; VCL: kf5 (cairo+xcb)
Locale: en-GB (en_GB.UTF-8); UI: en-GB
Calc: threaded