Wish Generics

wl

Well-Known Member
Licensed User
Longtime User
I'm often using List of instances of custom classes. It would be nice to have some kind of generics so that I can syntactically indicate what kind of instances a list could contain.

Thanks
 

stevel05

Expert
Licensed User
Longtime User
In case you hadn't thought of it, why not just name the lists appropriately? You'll soon find out if the list contains an element of the wrong type when you try to access it if you don't check it when reading.
B4X:
Dim MyCustomClassList As List
Dim MyCustomClass2List as List
 

LucaMs

Expert
Licensed User
Longtime User
Aside from the fact that that wouldn't be very practical, the point is that with a typed list (if you're talking about these) it can be used like (like Arrays):

B4X:
Type tPerson(Name As String, Age As Int)

Dim lstPersons As List ' ... As List Of(tPerson)
' init & add here

If lstPersons.Get(Index).Name = "Erel" Then
    Log("Lucky man")
End If

?
 

cklester

Well-Known Member
Licensed User
I've wanted to do this several times:

B4X:
Dim myList as List of MyClass
'some languages have you do something like
Dim myList as List<MyClass>

The work-around would be something like:

B4X:
Dim myList as List = LoadMyList
Dim mc as MyClass = myList.Get(0)
...

There's probably a more B4X way to do this, though.
 

wl

Well-Known Member
Licensed User
Longtime User
Yes ib
Aside from the fact that that wouldn't be very practical, the point is that with a typed list (if you're talking about these) it can be used like (like Arrays):

B4X:
Type tPerson(Name As String, Age As Int)

Dim lstPersons As List ' ... As List Of(tPerson)
' init & add here

If lstPersons.Get(Index).Name = "Erel" Then
    Log("Lucky man")
End If

?

Yes, I meant something like this. Using a namingconvention could be helpful but still has the disadvantage that you need to cast an element of a list to be useful and a wrong cast only returns in a runtime error, while compiletime would be much better.
 

wl

Well-Known Member
Licensed User
Longtime User
In case you hadn't thought of it, why not just name the lists appropriately? You'll soon find out if the list contains an element of the wrong type when you try to access it if you don't check it when reading.
B4X:
Dim MyCustomClassList As List
Dim MyCustomClass2List as List

This could be useful but I would like to name my variables based on what they contain (semantically) instead of what their type is.
 

gabriels

Member
Licensed User
Yes sir, just like me, I think that is very useful , when you are tying to standardize processes and the same time trying to keep integrity of types
I've wanted to do this several times:

B4X:
Dim myList as List of MyClass
'some languages have you do something like
Dim myList as List<MyClass>
 

JordiCP

Expert
Licensed User
Longtime User
A tricky workaround to achieve the same goal (detecting errors at compileTime) is to define small classes that do the trick to extend List for each type
The only disadvantage is that a class must be defined for each type (so it is not general) but can use them as a template for the next one just changing the object type.

Class CL_ListOfMyObjectType
B4X:
Sub Class_Globals
  Private mList as List
End Sub

Sub Initialize
  mList.Initialize
End Sub

Sub Add(element as MyObjectClass)    '<----
  mList.Add(element)
End Sub

Sub Get(index as Int) as MyObjectClass   '<----
  return mList.Get(index)
End Sub

Sub Size As int
  return mList.Size
End Sub

Sub GetList as List
  return mList      ' In case the primary list is needed ;-)
End Sub

' ...the rest of needed methods

main code
B4X:
Dim myListOfMyObjectType as CL_ListOfMyObjectType
myListOfMyObjectType.Initialize

Dim myObjectTypeInstance as MyObjectType
' ...
myListOfMyObjectType.Add(myObjectTypeInstance)
' ...
 

MathiasM

Active Member
Licensed User
A tricky workaround to achieve the same goal (detecting errors at compileTime) is to define small classes that do the trick to extend List for each type
The only disadvantage is that a class must be defined for each type (so it is not general) but can use them as a template for the next one just changing the object type.

Class CL_ListOfMyObjectType
B4X:
Sub Class_Globals
  Private mList as List
End Sub

Sub Initialize
  mList.Initialize
End Sub

Sub Add(element as MyObjectClass)    '<----
  mList.Add(element)
End Sub

Sub Get(index as Int) as MyObjectClass   '<----
  return mList.Get(index)
End Sub

Sub Size As int
  return mList.Size
End Sub

Sub GetList as List
  return mList      ' In case the primary list is needed ;-)
End Sub

' ...the rest of needed methods

main code
B4X:
Dim myListOfMyObjectType as CL_ListOfMyObjectType
myListOfMyObjectType.Initialize

Dim myObjectTypeInstance as MyObjectType
' ...
myListOfMyObjectType.Add(myObjectTypeInstance)
' ...

Yes this is what I do sometimes too. But it's a lot of overhead code ofcourse.
 

wl

Well-Known Member
Licensed User
Longtime User
Yes this is what I do sometimes too. But it's a lot of overhead code ofcourse.

Indeed sometime, it's quite some overhead ... needing an additional class in a separate code file ...
 
Top