The "Init" Statement

wonder

Expert
Licensed User
Longtime User
So, here's an idea (and as always, I'm going to be hated for it):

How about a new B4X statement named "Init" which would work like this:
B4X:
'Old Style:
Dim player As Knight
player.Initialize("Name", 12, -1, True)

'With Init:
Init player("Name", 12, -1, True) As Knight

'----------------------------------------------

'Old Style:
Dim myMap As Map
myMap.Initialize

'With Init:
Init myMap As Map

Just an idea... :)
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Shortcut for maps? You don't have to initialize it. That ultimately serve the same porpuse.
 

LWGShane

Well-Known Member
Licensed User
Longtime User
I understand why you want it. However it is a bit more complicated as there could be several Initialize method.
What about the New() keyword along with Optional parameters?

Ex:
B4X:
Sub Person (FirstName As String, Optional LastName As String)
In the event an optional parameter is not used, it should automatically be set to Null2. (Null2 should be created specifically for optional parameters.)


Which would result in:
B4X:
Dim Buffy As New Person ("Buffy")
'Or
Dim Buffy As New Person ("Buffy", "Summers")

'Instead of this:

Dim Buffy As Person
'With first name:
Buffy.Initialize("Buffy")
'With first AND last name:
Buffy Initialize2("Buffy", "Summers")

Note that I put the two Initialize statements in the same code block on purpose so I wouldn't have to create multiple code blocks.
 
Last edited:

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
this is called overload and because it is with the initialize it is a constructor overload.
Incorporating overloads seems quite a difficult task.

Do not like the idea of adding New, Vb.Net has it and it is always a mess when to use it and when not.
 

keirS

Well-Known Member
Licensed User
Longtime User
this is called overload and because it is with the initialize it is a constructor overload.
Incorporating overloads seems quite a difficult task.

Do not like the idea of adding New, Vb.Net has it and it is always a mess when to use it and when not.

The main issue I would imagine is that the B4i translator target language is Objective C and that doesn't support method overloading whereas Java and C++ do. Something to do with Objective C being dynamically typed IIRC.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The main issue I would imagine is that the B4i translator target language is Objective C and that doesn't support method overloading whereas Java and C++ do
This is not a problem. The B4X compiler can easily take care of it.

One issue with overloading is that B4X type system allows more implicit conversations compared to .Net or Java. This means that it can be more confusing to understand which actual method will be called.
The second "issue" is that the benefits of method overloading are quite small, and in some cases they do more harm than good.
For example .Net BinaryWriter.Write method (https://msdn.microsoft.com/en-us/library/system.io.binarywriter_methods(v=vs.110).aspx).
 

keirS

Well-Known Member
Licensed User
Longtime User
This is not a problem. The B4X compiler can easily take care of it.

One issue with overloading is that B4X type system allows more implicit conversations compared to .Net or Java. This means that it can be more confusing to understand which actual method will be called.
The second "issue" is that the benefits of method overloading are quite small, and in some cases they do more harm than good.
For example .Net BinaryWriter.Write method (https://msdn.microsoft.com/en-us/library/system.io.binarywriter_methods(v=vs.110).aspx).


I am not convinced by the second argument. That documentation just tells me I can call write with any of the .Net primitive types other than date plus pass arrays for two of the types and write compressed int's . It doesn't do it very well as a summary at the top of the page would have been good and saved me looking down all the methods. In B4x what do you suggest? because without overloading the two obvious answers to me are:

1. A sub for each primitive type and one for strings. Assuming I always passed an array with a start and end position for the region that's 11 subs

B4X:
Sub WriteBoolean(Bool As Boolean)

End Sub

Sub WriteByte(B As Byte)

End Sub

Sub WriteShort(Shrt As Short)

End Sub

Etc....



2. Have two subs and deal with the various possible types in the Select statement:

B4X:
Sub Write(O As Object)
       
 
Dim JavaType As String = GetType(O)
Select True
     Case JavaType.Contains("java.lang.Integer")

      Case JavaType.Contains("java.lang.Byte")
          
      Case JavaType.Contains("java.lang.Short")


      Etc...

End Select

End Select
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
I support Erel here.

We have the possibility to pass a string or an int to a function and the compiler will take care of that for us. In other languages this is a no no situation.

Any of the 2 methods described above are valid.

Anyway. Init doesn't sound a bad idea just to save some lines of code.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I am not convinced by the second argument. That documentation just tells me I can call write with any of the .Net primitive types other than date plus pass arrays for two of the types and write compressed int's . It doesn't do it very well as a summary at the top of the page would have been good and saved me looking down all the methods. In B4x what do you suggest? because without overloading the two obvious answers to me are:
In the case of .Net BinaryWriter it would have been much better to provide methods with different names to make it clear what you are writing.

Lets say that you need to write the following two bytes as part of a very large communication protocol: 0xFE, 0xFF
So you write:
bw.Write(0xFE);
bw.Write(0xFF);

Later the other machine crashes as the message is not properly formatted. After many hours you find out that instead of writing two bytes, it wrote two integers, with a total of 8 bytes.

This is a simple example. If there were variables involved then you need to check the type of each variable in order to understand which method is called.
 

wonder

Expert
Licensed User
Longtime User
Anyway. Init doesn't sound a bad idea just to save some lines of code.
:)

B4X:
Init variable As Int / Short / Long / Byte / Float / Double / String / Char
'The same as "Dim variable = ... As ...")


Init object As ObjectType
'Declares and initializes an object by
'calling its default constructor
'with no parameters


Init object(param1, param2, ...) As ObjectType
'Declares and initializes an object by
'calling its default constructor
'with the specified parameters


Init object.Constructor(param1, param2, ...) As ObjectType
'Declares and initializes an object by
'calling the specified constructor
'with the specified parameters


'Variations (default is Public, just like Dim):
Public  Init (...)
Private Init (...)
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Sorry, to potentialy save quite a lot of lines of code.

There are no constructors in B4X, we use initialize, some libraries have initialize2,3, etc but we can not create this second, third initializers with pure B4X so the init will have to stay with only one initialize and to create the others normally. that is the only but i find.
 

keirS

Well-Known Member
Licensed User
Longtime User
In the case of .Net BinaryWriter it would have been much better to provide methods with different names to make it clear what you are writing.

Lets say that you need to write the following two bytes as part of a very large communication protocol: 0xFE, 0xFF
So you write:
bw.Write(0xFE);
bw.Write(0xFF);

Later the other machine crashes as the message is not properly formatted. After many hours you find out that instead of writing two bytes, it wrote two integers, with a total of 8 bytes.

This is a simple example. If there were variables involved then you need to check the type of each variable in order to understand which method is called.

You can achieve the same thing by having a strict naming convention policy for your variables though.

I come from a background where I have done a lot of development in weakly typed and type unsafe languages. Languages where doing stuff like this is perfectly valid:
B4X:
ABC = 1
ABC = "12345"
ABC = CREATOBJECT("MyObject")
ABC = .T.
ABC = 9.555

So using Systems Hungarian notation is pretty much a must for variable naming in order to debug large systems. Therefore I know what data type I am writing by the variable name. If in the debugger I see a variable which has a type that does not match it's name then I know I have a problem. This is frustratingly difficult to do in B4X though as the debugger does not show the type for variables. Did the debugger in older versions of B4A show this?
 
Top