It would be useful to have a CreateList, similar to CreateMap.
Having it, I could write:
B4X:
Type tTest(Elems As List)
Private objTest As tTest
objTest = CreatetTest(CreateList()) ' or a list of "fake numbers", like -1: CreateList(-1, -1, -1)
B4X:
Public Sub CreatetTest (Elems As List) As tTest
Dim t1 As tTest
t1.Initialize
t1.Elems = Elems
Return t1
End Sub
Currently I must write:
B4X:
Type tTest(Elems As List)
Private objTest As tTest
objTest = CreatetTest(Null)
B4X:
Public Sub CreatetTest (Elems As List) As tTest
Dim t1 As tTest
t1.Initialize
If Elems.IsInitialized = False Then
Elems.Initialize
End If
t1.Elems = Elems
Return t1
End Sub
Note that if I wrote:
CreateTest (Array As Int ())
the list would become immutable (an Array, for that matter)
Note also (for the picky ones like me ?): obviously I would never create a custom type like that, with only one "field".
BTW in the project I am developing I had to go back to Maps instead of Lists (I had changed the type of a "field" of a custom type from Map to List but I had to go back to the previous type)
Dim myList As List = CreateList(obj1 : obj2 : obj3)
Dim myList As List = CreateList() 'would make every single B4X user happy while also not confusing a single one
Dim myMap As Map = CreateMap("key1" : obj1, "key2" : obj2, "key3" : obj3) 'is already similar in syntax
Doable now with a slight variant to accommodate a variable number of arguments:
B4X:
Sub Test
Dim aList As List = CreateList(Array("a", 1, True))
Log(aList.Get(0) & TAB & aList.Get(1) & TAB & aList.Get(2))
End Sub
Private Sub CreateList(ar() As Object) As List
Dim a As List
a.Initialize
For Each obj As Object In ar
a.Add(obj)
Next
Return a
End Sub
But I too would like the CreateList() rather than CreateList(Array())
I feel like that if you're going to add syntactic sugar for one function, you may as well generalise and make that sweet style available for all functions.
Something like: if the function parameter list is (or ends with?) an array-of-objects parameter, then that function can be called with a variable number of parameters, that get bundled up into the array-of-objects.
B4X:
Sub CreateList(VarPar() As Object) As List
Dim L As List
L.Initialize
For Each O As Object In VarPar
L.Add(O)
Next
Return L
End Sub
Dim Test As List = CreateList("One", 2, "Three", 4)
About passing an array to a CreateList method, yes I really like lean code and sometimes it's not only about less lines, it's about less characters so getting rid of the Array syntax would be great. After all the CreateMap method is already doing that.
For now I use a "Lists" module with methods such as New() and New2(objects() as Object).
@emexes
Or perhaps a new type "Arguments": a list that is populated with the arguments of the calling sub.
There are already several examples in B4X.
CreateMap of course, but also the "Array As ..." functions, as well as some of the Designer Extension functions.