TIL - Kotlins' truly OOP
Ayman Patel
Back End Engineer @ MastercardLinks
- Type Witness
It ensures OOP by 3 virtues
- Immutable
Use val not var
Not null
No static access
Static access is BAD for OOP since OOP originally is about object-passing and static objects makes it a global stateful object.
But, Junit 4 @Before is staticl so if you want to write a ot of boilerplate to allow static instantiation
Type Witness in Generic#
Suppose you want to invoke the method processStringList with an empty list. In Java SE 7, the following statement does not compile:
The Java SE 7 compiler generates an error message similar to the following:
This is no longer necessary in Java SE 8. The notion of what is a target type has been expanded to include method arguments, such as the argument to the method processStringList. In this case, processStringList requires an argument of type List. The method Collections.emptyList returns a value of List, so using the target type of List, the compiler infers that the type argument T has a value of String. Thus, in Java SE 8, the following statement compiles: