List Functionality (cannot Type a List)

NFOBoy

Active Member
Licensed User
Longtime User
Just wondering if I'm missing something anywhere.

In trying to learn Java to understand some things better, I really liked the fact that Lists could be Typed to some object, so that when you get an index of the List, no need to dim a new object (similar to B4A for each x as y in list, but for any object in the list)

Now, I don't have reason to store many lists where I have different types of objects in the list, and am wondering if that exists in B4A already? If not, is there any reason that library being created for B4A would be ungainly difficult? (If not, that will be my first attempt at porting a Java library over, unless somebody's already done it, and I can't find it with search)

Ross
 

NFOBoy

Active Member
Licensed User
Longtime User
Erel,

true, but the power of the list array in Java, is the ability to cast it to whatever Type desired.

So, if I have a custom user Type Person (name as String, age as long, weight as float, canBeChosen as Boolean)

In B4A, I already know that I can then assign it using your example. However, there is no checking of the Type when adding to the List (so can add anything to the List, vice explicit Casting), which means cannot do this

Dim p1, p2, p3, p4 as Person

'do initialization and set parameters of p1-p4
Dim listPeople as List <Person>

listPeople.add(p1)
listPeople.add(p2)
listPeople.add(p3)
listPeople.add(p4)



later in code...

sub setCanBeChosen
 
Upvote 0

NFOBoy

Active Member
Licensed User
Longtime User
Erel,

true, but the power of the list array in Java, is the ability to cast it to whatever Type desired.

So, if I have a custom user Type Person (name as String, age as long, weight as float, canBeChosen as Boolean)

In B4A, I already know that I can then assign it using your example. However, there is no checking of the Type when adding to the List (so can add anything to the List, vice explicit Casting), which means cannot do this

B4X:
Dim p1, p2, p3, p4 as Person

'do initialization and set parameters of p1-p4
Dim listPeople as List <Person>

listPeople.add(p1)
listPeople.add(p2)
listPeople.add(p3)
listPeople.add(p4)

Then later in code ..

B4X:
sub getRandomPersonName as String
   Return listPeople(rnd(0,listPeople.size)).name
end sub
Instead we do something like
B4X:
sub getRandomPersonName as String

  dim somePerson as Person
  somePerson = listPeople(rnd(0,listPeople.size))
  Return somePerson.name

end sub
This is just something that I liked in Java and thought it would be nice to have here with Lists in general (just like we have now with Classes where the Returned object can be de-referenced with the ".")

I just wondered if writing a Library to mimic this would be possible, as I think that it might not do to the fact that I don't know how to set a return type dynamically such that the IDE can recognize it. As classes we build in B4A are set to be specific return types, therefore the IDE can de-reference it as stated. If it can be done in building a library from Java source... then it would be an interesting library (for me) to build.

Ross
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I just wondered if writing a Library to mimic this would be possible
No, you cannot implement this in a library. This is a compile time check made by the Java compiler. As Java is statically typed the compiler must know the type of each object to produce the correct code. To implement it in B4A it would need to be implemented in the B4A compiler.

As you may know this ability is an example of "generics" whose intention is to provide better compile time type checking and so, hopefully, prevent run time errors occurring.
 
Upvote 0
Top