LucaMs Expert Licensed User Longtime User May 16, 2022 #1 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".
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 May 16, 2022 #2 LucaMs said: obviously I would never create a custom type like that, with only one "field". Click to expand... 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: Code: Public Sub CreateMutableList(ImmutableList As List) As List Dim l1 As List l1.Initialize l1.AddAll(ImmutableList) Return l1 End Sub
LucaMs said: obviously I would never create a custom type like that, with only one "field". Click to expand... 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: Code: Public Sub CreateMutableList(ImmutableList As List) As List Dim l1 As List l1.Initialize l1.AddAll(ImmutableList) Return l1 End Sub
LucaMs Expert Licensed User Longtime User May 16, 2022 #3 Erel said: I think that in 99% of the cases Array is good enough Click to expand... Yes, I almost agree: I think that in 99% of cases Lists are better than Arrays ?
Erel said: I think that in 99% of the cases Array is good enough Click to expand... Yes, I almost agree: I think that in 99% of cases Lists are better than Arrays ?
LucaMs Expert Licensed User Longtime User May 16, 2022 #4 LucaMs said: Yes, I almost agree: I think that in 99% of cases Lists are better than Arrays ? Click to expand... 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)
LucaMs said: Yes, I almost agree: I think that in 99% of cases Lists are better than Arrays ? Click to expand... 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)
TelKel81 Active Member Licensed User Longtime User Aug 21, 2023 #5 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
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 Aug 21, 2023 #6 @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())
@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())
E emexes Expert Licensed User Longtime User Aug 22, 2023 #7 William Lancee said: Actually the parallel syntax would be B4X: Dim aList as LIst = CreateList(obj1, obj2, obj3) But I too would like the CreateList() rather than CreateList(Array()) Click to expand... 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.
William Lancee said: Actually the parallel syntax would be B4X: Dim aList as LIst = CreateList(obj1, obj2, obj3) But I too would like the CreateList() rather than CreateList(Array()) Click to expand... 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 Longtime User Aug 22, 2023 #8 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).
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 Aug 22, 2023 #9 @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.
@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.