Wish CreateList

LucaMs

Expert
Licensed User
Longtime User
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".
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
obviously I would never create a custom type like that, with only one "field".
This can actually is useful in some cases. It allows you to pass a field by reference.

I think that in 99% of the cases Array is good enough. For the other cases you can use:
B4X:
Public Sub CreateMutableList(ImmutableList As List) As List
 Dim l1 As List
 l1.Initialize
 l1.AddAll(ImmutableList)
 Return l1
End Sub
 

TelKel81

Active Member
Licensed User
This format would be amazing
B4X:
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
 

William Lancee

Well-Known Member
Licensed User
Longtime User
@Martin D.

Actually the parallel syntax would be
B4X:
    Dim aList as LIst = CreateList(obj1, obj2, obj3)
Since there are no keys.

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())
 

emexes

Expert
Licensed User

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)

Or maybe something like the f(p, q, r, ...) of C.
 

TelKel81

Active Member
Licensed User
You are so right about the proper syntax (commas)

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).
 

William Lancee

Well-Known Member
Licensed User
Longtime User
@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.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…