If we take a look at our Aquarium class of the previous example, we can see that the generic type is only ever returned by the property water supply.We didn't define any functions that take a value of type t as a parameter. Kotlin let's us define out types for exactly this case. Out types are type parameters that only ever occur in return values of function s, or on Val properties . If we try to pass an out type as a parameter to a function, Kotlin will give us a compiler error. Once we make a generic type and out type, Kotlin can infer extra information about where our types are safe to use. For example, let's declare a function, addItemTo that expects an aquarium of water supply.We can call addItemTo on an aquarium of tap water. Kotlin can ensure that addItemTo won't do anything unsafe with a generic, because it's declared as an out type. Main.kt function import generics.Aquarium // File: Main.kt // Programmer: Engineer Nolverto Urias Obeso //...
Comments
Post a Comment