'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
What about the New() keyword along with Optional parameters?I understand why you want it. However it is a bit more complicated as there could be several Initialize method.
Sub Person (FirstName As String, Optional LastName As String)
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")
Um... Why???I understand why you want it
This is my prerogative(and as always, I'm going to be hated for it)
Off topic, why should I use createmap()?
I'm not familiar with it.
Dim m As Map
m.Initialize
m.Put("a", 1)
m.Put("b", 2)
m.Put("c", 3)
'or
Dim m As Map = CreateMap("a": 1, "b": 2, "c": 3)
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.
This is not a problem. The B4X compiler can easily take care of it.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).
Sub WriteBoolean(Bool As Boolean)
End Sub
Sub WriteByte(B As Byte)
End Sub
Sub WriteShort(Shrt As Short)
End Sub
Etc....
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
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.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:
Anyway. Init doesn't sound a bad idea just to save some lines of code.
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 (...)
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.
ABC = 1
ABC = "12345"
ABC = CREATOBJECT("MyObject")
ABC = .T.
ABC = 9.555
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?