In all the examples I am ready this kind of function definition keeps popping up:
def firstElementInList[T](l: List[T]): T
I'm used to seeing List[Int]
so it would be a list of integers. In this case I'm assuming that T
is any type (please correct me if im wrong). What I'm really getting caught up on is the [T]
right after firstElementInList
This answer is not useful. Show activity on this post. T is an "unbound" type. In other words, List<T> is shorthand for "list of things". This means you can use the same code to make a "list of people" a "list of dates" or a "list of accounts", you just provide the constructor.
In the above example, List<int> primeNumbers = new List<int>(); creates a list of int type. In the same way, cities and bigCities are string type list. You can then add elements in a list using the Add() method or the collection-initializer syntax.
Add(new string[] { "Id", "Name", "Age" }); for(int i=0; i<list. Count; i++) for(int j=0; j<list[i]. Count(); j++) Console. WriteLine(list[i][j]); // simpler with foreach foreach(var l in list) foreach(string s in l) Console.
< T > is a conventional letter that stands for "Type", and it refers to the concept of Generics in Java. You can use any letter, but you'll see that 'T' is widely preferred. WHAT DOES GENERIC MEAN? Generic is a way to parameterize a class, method, or interface.
IT is just a way to tell: this function references one generic type T
(you are right T
is any type).
If you have multiple methods inside one class:
def firstElementInList[T](l: List[T]): T = l.head
def lastElementInList[T](l: List[T]): T = l.last
then each method has its own T
type, so you can call first method with a list of String
s and a second one with a list of Int
s.
However the whole class enclosing both of these methods can have type as well:
class Foo[T] {
def firstElementInList(l: List[T]): T = l.head
def lastElementInList(l: List[T]): T = l.last
}
In this case you pick type during Foo
object creation:
val foo = new Foo[String]
and the compiler will prevent you from calling instance methods of foo
with any other type than List[String]
. Also note that in this case you no longer need type [T]
for a method - it is taken from the enclosing class.
T is an "unbound" type. In other words, List<T>
is shorthand for "list of things".
This means you can use the same code to make a "list of people" a "list of dates" or a "list of accounts", you just provide the constructor
List<Person> people = new List<Person>();
Which will bind T
to People. Now when you access the List
it will guarantee that everywhere the previously unbound T
existed, it will act as if it was written with "People" in that position. For example, public T remove(int)
will return a T
which is bound to "People" in the people list. This avoids the need to add explicit casts. It also guarantees that the only items within the List
are at least People.
List<Person> people = new List<Person>(); // T is bound to "People"
List<Account> accounts = new List<Account>(); // T is bound to "Account"
Person first = people.remove(0);
Account firstAccount = accounts.remove(0);
// The following line fails because Java doesn't automatically cast (amongst classes)
Account other = people.remove(0);
// as people is a List<T> where T is bound to "People", people.remove(0) will return
// a T which is bound to People. In short it will return something that is at least an
// instance of People. This doesn't cast into an account (even though there are other
// lists which would).
Note that the at least People comment is an indicator that the list might contain multiple objects, but all the objects must be sub-classes of People.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With