2019-07-14

19: Untold or Incorrectly Told Basics of LibreOffice Basic

<The previous article in this series | The table of contents of this series | The next article in this series>

I cannot find any document that satisfactorily addresses some of my basic concerns about LibreOffice/OpenOffice Basic. Such concerns are tackled here.

Topics


About: UNO (Universal Network Objects)
About: LibreOffice
About: Apache OpenOffice
About: LibreOffice Basic
About: Apache OpenOffice Basic

The table of contents of this article


Starting Context



Target Context


  • The reader will know some so far untold or incorrectly told basics of LibreOffice or Apache OpenOffice Basic.
Stage Direction
Here are Hypothesizer 7, Objector 19A, and Objector 19B in front of a computer.


Orientation


Hypothesizer 7
In this article, I will attempt to clarify some of the so far untold or incorrectly told basics of LibreOffice or Apache OpenOffice Basic.

Objector 19B
I wonder why all the documents on LibreOffice Basic are so unsatisfactory . . .

Hypothesizer 7
Madam, I cannot tell about all the documents because I have not read all of them, but at least all the documents I have read on LibreOffice or Apache OpenOffice Basic do not address some of what I deem to be essential.

Objector 19B
So, I wonder why.

Hypothesizer 7
I cannot help but deem that that is because Basic is not supposed to be taken so seriously; probably, there is not much demand for serious documents.

Objector 19B
So, Basic is a joke . . .

Hypothesizer 7
. . . Whether it is a joke or not, as far as I make some programs in it, I require the accurate knowledge on at least the essential aspects of it. You know, however small my programs are, any lack of the necessary knowledge can trip me enigmatically. . . . Well, besides that, I admit, I cannot stand blindly doing what I do not understand.

Objector 19B
I wonder whether my concerns coincide with yours.

Hypothesizer 7
What I address here are quite primary ones.

For example, what any variable of each type can contain?

Objector 19B
. . . I think, all the documents I have read at least tell that, for example, any 'Integer' variable can contain an integer.

Hypothesizer 7
They does not tell whether the variable can contain the address of a datum or a datum itself, do they?

Objector 19B
A document tells that, for example, the 'Integer' type copies by value.

Hypothesizer 7
That is an example of how such a document is far from being passable. . . . Such a statement is essentially nonsense. You know, any 'Integer' or whatever function argument is a reference by default (so, passing any 'Integer' datum into such a function argument does not "copy by value"); so, you cannot say "the 'Integer' type copies by value" although you may be able to say "the assignment operator on any 'Integer' variable copies the value". . . . In fact, "copies by value" is not any attribute of any variable type, but an attribute of, for example, the assignment operator (it may be an attribute of a function return).

Objector 19B
Ah, that's true.

Hypothesizer 7
Besides, the value of any pointer is an address, and the assignment operator on it copies the value (the address). So, saying "copies by value" is not telling anything about whether the variable is a pointer, a normal variable, or whatever.

Objector 19B
That's true too. . . . But most readers of such a document do not or are not supposed to know or care what 'pointer' or 'address' is.

Hypothesizer 7
I guess so, but for any serious programmer, that is a point that has to be crystal-clear.

Objector 19B
Hmm . . .

Hypothesizer 7
For example, do you know that any user-defined datum type variable is a pointer?

Objector 19B
Huh? A document says that it copies by value . . .

Hypothesizer 7
That document is correct in that the address, which is the value, is copied by the assignment operator, but if you understand that the user-defined datum type variable is a normal, non-pointer variable, that is a misunderstanding.

Objector 19B
Everybody will understand that it is a value type . . .

Hypothesizer 7
But it is a pointer, as you can easily confirm in this code (actually, the misnomers, "value type" and "reference type", should be ditched away, in my opinion).

@LibreOffice Basic Source Code
Type UserDefinedTypeA
	i_stringA As String
End Type

Sub testUserDefinedDatumTypeVariable ()
	Dim l_userDefinedTypeAInstanceInUserDefinedTypeA0 As UserDefinedTypeA
	Dim l_userDefinedTypeAInstanceInUserDefinedTypeA1 As UserDefinedTypeA
	l_userDefinedTypeAInstanceInUserDefinedTypeA1 = l_userDefinedTypeAInstanceInUserDefinedTypeA0
	l_userDefinedTypeAInstanceInUserDefinedTypeA0.i_stringA = "AAA"
	MsgBox (l_userDefinedTypeAInstanceInUserDefinedTypeA1.i_stringA)
End Sub

Stage Direction
Hypothesizer 7 executes the 'testUserDefinedDatumTypeVariable' subroutine, which shows a 'AAA' message box.

Objector 19B
Huh? Why is 'AAA' in 'l_userDefinedTypeAInstanceInUserDefinedTypeA1.i_stringA'?

Hypothesizer 7
Because 'l_userDefinedTypeAInstanceInUserDefinedTypeA1' is a pointer and the assignment of 'l_userDefinedTypeAInstanceInUserDefinedTypeA0' to 'l_userDefinedTypeAInstanceInUserDefinedTypeA1' has copied the address of the 'l_userDefinedTypeAInstanceInUserDefinedTypeA0' datum into the 'l_userDefinedTypeAInstanceInUserDefinedTypeA1' variable. . . . Please do not be fooled by the fact that the defining any user-defined type variable automatically creates an instance of the corresponding user-defined datum type and the address is set into the variable.

Objector 19B
Huh? What is happening?

Hypothesizer 7
'Dim l_userDefinedTypeAInstanceInUserDefinedTypeA0 As UserDefinedTypeA' amounts to 'UserDefinedTypeA l_userDefinedTypeAInstanceInUserDefinedTypeA0 = new UserDefinedTypeA ()' in Java, which does not make 'UserDefinedTypeA' a so-called (misnomer-ly) "value type".

Objector 19B
. . .

Hypothesizer 7
In fact, without understanding that any user-defined datum type variable is a pointer and what 'pointer' is, the behaviors of the variable cannot be accurately understood, whether the reader cares what 'pointer' is, or not. . . . You know, 'pointer' and 'reference' are different things and behave differently, and any pointer has to be understood as a pointer, not any reference.

More generally speaking, each of all the LibreOffice or Apache OpenOffice documents I have read does not appropriately distinguish 'datum', 'variable', 'expression', and 'value', and mixing up those concepts inevitably ruin the document (in fact, that problem is quite prevalent across all the programming languages I have read some documents on).

Objector 19B
. . .

Objector 19A
Your 'Starting Context' says that I am supposed to have "read a supposed mostly comprehensive document on the basics of LibreOffice or Apache OpenOffice Basic" . . .

Hypothesizer 7
Sir, that means that this article does not comprehensively cover the basics of LibreOffice or Apache OpenOffice Basic; this article tackles only some egregiously untold or incorrectly told aspects, on the supposition that the reader has read an average document that seems to be meant to rather comprehensively explain the basics of LibreOffice or Apache OpenOffice Basic.

Objector 19A
. . .


Main Body


1: What Each of the Variable Types Really Is


Hypothesizer 7
First, let me declare that we have to clearly distinguish 'darum', 'variable', and 'value'. In fact, when I talk of 'variable type', I definitely mean 'variable type', not 'datum type'.

Second, let us distinguish 'non-argument variable' and 'argument variable'. Any argument variable can be a reference variable (in the C++ meaning (so-called "reference type variable" in Java is really a pointer)) or a non-reference variable. On the other hand, there is no such a thing as a reference non-argument variable.

Passing something into any non-reference argument variable behaves the same way as assigning it into a non-argument variable.

So, we will discuss 'non-argument variable' hereafter.

There are these categories of variable types: Basic-built-in types (like 'String'), UNO structs, 'Object', 'Variant', and user-defined types.

Any Basic-built-in type variable is a normal variable (not any pointer or reference).

Any UNO struct variable is a normal variable.

Any user-defined type variable is a pointer.

'Object' and 'Variant', actually, defy such a categorization.

Objector 19A
What do you mean by "defy"?

Hypothesizer 7
Any 'Object' variable can contain a UNO struct datum (not its address), a UNO object address, or a user-defined type datum address, at a time.

Objector 19A
So, an 'Object' variable sometimes contains a datum and sometimes contains an address . . .

Hypothesizer 7
Please note that although an official document recommends using any 'Object' variable only for containing user-defined type data, the same official document profusely uses 'Object' variables for containing other data. . . . Honestly, I do not accept such an irresponsible recommendation, and so I take a stance that using any 'Object' variable for containing other data is OK (in fact, I cannot find any example that it causes a trouble).

Objector 19B
. . . You should accept what it says, obediently, as there is no harm in using 'Variant' instead.

Hypothesizer 7
In fact, I see some harm in using any unnecessarily comprehensive variable.

Objector 19B
You are a nasty fellow . . .

Hypothesizer 7
Any 'Variant' variable can contain a Basic-built-in type datum (not its address), a UNO struct datum (not its address), a UNO object address, or a user-defined type datum address, at a time.

Objector 19A
Hmm . . .


2: Any Function Return Is Not Any Reference


Hypothesizer 7
As no document I have read so far addresses this, let us clarify this: any function return Is not any reference.

. . . Is that matter-of-course? . . . Well, as any argument variable is a reference by default, at least I wonder "then, how about 'return'?".

In fact, any function return returns a Basic-built-in type datum (not its address), a UNO struct datum (not its address), a UNO object address, or a user-defined type datum address, at a time.


3: Any Variable Does Not Seem to Be Able to Be Initialized at an Arbitrary Value at the Definition


Hypothesizer 7
Any variable does not seem to be able to be initialized at an arbitrary value at the definition.

Objector 19A
Huh?

Hypothesizer 7
I mean, we cannot do like 'Global s_integerA As Integer = 2', 'Global s_integerA = 2', or whatever.

Certainly, as for any local variable, we can do like 'Dim l_integerA As Integer : l_integerA = 2', but that does not work for any global variable, for ':' is just connecting 2 lines (outside any function or subroutine, the 2nd line is invalid).

Objector 19A
. . . Can't we really initialize any variable at an arbitrary value, which I have thought to be a universal feature for all the programming language?

Hypothesizer 7
I do not know for sure; just, no document I have read even mentions that feature.

The problem is that there is no proper timing to initialize any global variable at my desired value: in order to initialize the global variable at my desired value, I have to do it in a function (initializing function), but as there are some multiple functions, each of which can be called first, which is the function in which the initializing function should be called? As far as I can fathom, I have to call the initializing function in every other function (the initializing function will have to exert a control that it initializes the variable only once), which is a cumbersome solution.


4: The Scope of Any Function or Subroutine


Hypothesizer 7
Although no document I have so far read adresses this, any function or subroutine seems automatically and inevitably global.

Objector 19A
"inevitably"?

Hypothesizer 7
Any function or subroutine can be just called from another module, even if we qualify it 'Private' or do not qualify it at all.

Objector 19A
. . . Basic at least points out that 'Private' can't be used there, doesn't it?

Hypothesizer 7
No, actually, it is just ignored.

Objector 19A
That sucks. . . . What happens if there are 2 functions of the same name?

Hypothesizer 7
The one found first seems to be used.

Objector 19A
. . . Which will be found first?

Hypothesizer 7
I do not know the exact rule. . . . It seems not meaningful to try to know such a rule: we should rather avoid using any duplicate name, although Basic does not kindly alert any duplication . . .


5: The Scope of Any User-Defined Type


Hypothesizer 7
It seems that the name of any user-defined type is valid only inside the module in which the user-defined type is defined.

Objector 19A
. . . That is a severe handicap. . . . Any user-defined type will be able to be used only in the defining module . . .

Hypothesizer 7
Actually, I did not say that the user-defined type is valid only inside the defining module, but said that the name of it is so.

Objector 19A
Huh? There is no difference there, isn't it?

Hypothesizer 7
Yes, there is: if a function in the defining module returns an instance of the user-defined type, any another module that calls the function can access the instance, for example, through an 'Object' variable. In fact, in a previous article, I have introduced the concept of such constructors.

Objector 19A
An odd specification: the name cannot be used, but the datum can be used . . .


6: The Lifetime of Any User-Defined Type Instance?


Objector 19A
In your constructor, 'ClassA_ClassA (a_argument0 As String)', 'l_this' is a local instance, and the constructor returns the address of the local instance, according to your explanation of 'function return'; is that OK?

Hypothesizer 7
As I have said, we have to distinguish 'variable' and 'datum'. . . . In fact, 'l_this' itself is not any "instance", but a pointer variable. Certainly, the variable is local, but whether the instance pointed by the variable is local or not is another matter. . . . As no document I have so far read addresses that essential matter, I cannot support my guess with any document, but inferring from the behavior of LibreOffice Basic, I guess that any user-defined type instance is in the heap, not in the stack, and it is OK that the function returns the address.

Objector 19A
Then, when will the instance be cleared up? Garbage-collected?

Hypothesizer 7
I can only guess so, because no document I have so far read addresses that important concern.


7: How to Reset Any 'Object' Variable or Any 'Variant' Variable


Hypothesizer 7
One naturally wants to reset any 'Object' variable when the variable becomes to point to or contain nothing semantically.

Objector 19B
Actually, that is one of my unsolved concerns. I thought, naturally, that assigning 'Null' would reset the variable, but that causes an error!

Hypothesizer 7
Yes. I do not understand why Basic does not allow such a natural syntax, but there are 2 ways (as far as I know) that reset any 'Object' variable.

The way 1 is to pass the variable through the 'Erase' function.

Objector 19B
Oh? . . . Certainly, a document has cited the function as a function that frees the object memory, but I couldn't identify it as a way to set the variable at 'Null' . . .

Hypothesizer 7
The way 2 is to assign 'Nothing' to the variable.

Objector 19B
Huh? What is 'Nothing'? How is it different from 'Null'?

Hypothesizer 7
Well, I have found a document that explains the difference, but the claimed difference does not seem to fit the really. . . . In fact, in the initial state, the variable is both 'Nothing' and 'Null', and after 'Nothing' has been assigned to the variable, the variable is also both 'Nothing' and 'Null'; I do not know how an 'Object' variable can be not 'Null', but 'Nothing', or vice versa. If it cannot be so, I will have to decide that there is no difference.

On the other hand, any 'Variant' variable can be reset by passing the variable through the 'Erase' function.

And the variable can be set at 'Null' by assigning 'Nothing' into the variable.


8: The Function, 'VarType', Is Not About 'Variable Type', Mostly


Hypothesizer 7
There is a function, 'VarType', and despite the name and whatever some documents claim, the function is not about 'variable type', mostly.

Objector 19A
"mostly"?

Hypothesizer 7
For example, 'VarType (1)' returns '2', meaning 'being an integer', but claiming "it is about a variable type" is absurd because there is no variable involved there: '1' is not any variable.

Objector 19A
It is a datum type, of course.

Hypothesizer 7
When any 'Variant' type variable is specified as the function argument, the function reports the type of the datum set in the variable or reports that the variable is empty, while the variable type of any 'Variant' variable cannot be but 'Variant'.

Objector 19A
So, the function isn't about 'variable type' at all, is it?

Hypothesizer 7
However, only when any 'Object' type variable is specified as the function argument, the function reports just that the variable is an 'Object' type, even if the variable is at 'Null'.

Objector 19A
An odd behavior. . . . What does 'VarType (Null)' return?

Hypothesizer 7
It returns '1', meaning 'being 'Null''.

Objector 19A
Odd . . .


9: A Supplement to Some Insufficient Explanations on Modifying Any UNO Object 'Struct' Property


Objector 19A
A document claims that as 'struct's copy by value, a piece of code like 'l_unoObjectA.structPropertyA.structMemberA = "A datum"' doesn't modify the UNO object property, but a copy of the UNO object property, where "l_unoObjectA" is the UNO object, "structPropertyA" is the 'struct' property, and "structMemberA" is a member variable of the 'struct'.

I perfectly understand that if the property was assigned to a variable, the variable would contain a copy of the property value, but the above code doesn't assign the property to any variable! It is directly accessed! Why is it copied?

Hypothesizer 7
Ah, I do not know the context in which the explanation is placed in the document, but if the document has not adequately explained the nature of the syntax of 'UNO object property access in Basic', that explanation will have to be deemed to be lacking in an indispensable part.

Objector 19A
What do you mean?

Hypothesizer 7
The syntax like "l_unoObjectA.structPropertyA.structMemberA" is really a syntax sugar for 'l_unoObjectA.getStructPropertyA ().structMemberA', calling the getter method; without that preliminary explanation, the explanation you have cited does not make sense.

Objector 19A
Well . . .

Hypothesizer 7
"l_unoObjectA.structPropertyA" in the above code is really a function return, and according to the behaviors of 'function return', it is a copy of the property datum.

Although the official document claims "Objects are handled as references and structs as values", that is not any accurate statement: any function return is not any reference as is explained in a previous section. Any UNO object return is really an address (let us appropriately distinguish 'pointer', 'address', and 'reference').

Objector 19A
Well . . .

Hypothesizer 7
On the other hand, a syntax like "l_unoObjectA.structPropertyA = l_structA" is really a syntax sugar for 'l_unoObjectA.setStructPropertyA (l_structA)', calling the setter method.

Objector 19A
Hmm . . .


10: The Conclusion and Beyond


Hypothesizer 7
They are the minimum I have felt I have to clarify, for now.

As I do not use Basic extensively (it is not my favorite programming language because of its many aspects), there will be more obscure things I have not noticed yet. I may tackle those things in another opportunity.


References


<The previous article in this series | The table of contents of this series | The next article in this series>