Bug 161315 - 'Empty' can't be used as a default value for arguments (Option Compatible)
Summary: 'Empty' can't be used as a default value for arguments (Option Compatible)
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
Inherited From OOo
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: Macro-StarBasic
  Show dependency treegraph
 
Reported: 2024-05-29 04:53 UTC by Mike Kaganski
Modified: 2024-07-03 21:01 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Kaganski 2024-05-29 04:53:18 UTC
Consider the code:

  Option Compatible

  sub foo(Optional v As Variant = Empty)
    MsgBox v
  end sub

  sub bar
    foo
  end sub

In MS Office (Word/Excel, indeed omitting the 'Option Compatible', which is LibreOffice Basic compatibility flag), when running bar, it shows an empty message.

In LibreOffice, this code doesn't compile, giving syntax error on the 'Empty' default argument value; the same happens with 'Option VBASupport 1'. Replacing the 'Empty' with other values, like '1' or '""', succeeds.

There is no reason why the 'Empty' shouldn't work here.
Comment 1 Andreas Heinisch 2024-06-12 05:41:19 UTC
Repro in:
Version: 7.6.1.2 (X86_64) / LibreOffice Community
Build ID: f5defcebd022c5bc36bbb79be232cb6926d8f674
CPU threads: 16; OS: Windows 10.0 Build 22631; UI render: Skia/Raster; VCL: win
Locale: en-GB (de_DE); UI: en-GB
Calc: CL threaded
Comment 2 Jean-Pierre Ledure 2024-06-12 09:13:02 UTC
Consider next code:

  sub foo(Optional a, Optional b)
    MsgBox IsEmpty(a)
  end sub

  sub bar
    foo(b := 1)
  end sub

- (1) The message box returns True without any option, or with Option Compatible
- (2) A syntax error is displayed with Option VBASupport 1

In other words when (1) and the call uses the := syntax, the skipped arguments contain Empty, not "Missing". Empty means something in this matter.

That's the reason why in ScriptForge, everywhere I test the presence of an argument to set a default value (hundredths of times ...) I use the construction
  If IsMissing(a) Or IsEmpty(a) Then a = ...

Please do not change the actual behaviour without good reasons.
Comment 3 Mike Kaganski 2024-06-12 09:22:10 UTC
(In reply to Jean-Pierre Ledure from comment #2)
> Please do not change the actual behaviour without good reasons.

Which change is meant?
Comment 4 Jean-Pierre Ledure 2024-06-12 10:15:54 UTC
(In reply to Mike Kaganski from comment #3)

> Which change is meant?

Well, a potential patch that would implement Empty as being a valid default value.
This could create a conflict with Empty meaning missing in some cases in existing scripts.
Comment 5 Mike Kaganski 2024-06-12 10:18:15 UTC
(In reply to Jean-Pierre Ledure from comment #2)
> - (1) The message box returns True without any option, or with Option
> Compatible
> - (2) A syntax error is displayed with Option VBASupport 1
> 
> In other words when (1) and the call uses the := syntax, the skipped
> arguments contain Empty, not "Missing". Empty means something in this matter.

This is a bug. It must be fixed. It is orthogonal to this issue.

(In reply to Jean-Pierre Ledure from comment #4)
> a potential patch that would implement Empty as being a valid default
> value.
> This could create a conflict with Empty meaning missing in some cases in
> existing scripts.

This is not a reason to keep the two bugs.
Comment 6 Rafael Lima 2024-07-03 21:01:56 UTC
I'd say the problem is not only the inability to use Empty as a default argument, but also that the interpreter assumes that an argument is Empty in a peculiar manner. Consider the following code:

sub foo2(Optional v As Variant)
    MsgBox IsEmpty(v)
    MsgBox IsMissing(v)
end sub

sub bar2
    foo2
end sub

This will return False and True which to me is the expected behavior. However, the following code:

sub foo3(Optional v As Variant, Optional text As String)
    MsgBox IsEmpty(v)
    MsgBox IsMissing(v)
end sub

sub bar3
    foo3(text := "dummy")
end sub

... will return True and False, which is the opposite of what I would expect, since "v" is missing and it was not defined as Empty. See bug 143706 (already referred here).

I guess we need to fix bug 143706 alongside with this one to have a coherent behavior.