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 //...
A note about the view binding feature (Android Studio 3.6+) Since Developing Android Apps with Kotlin has launched, a new feature, view binding, has been introduced and is available in Android Studio 3.6 and higher. We don't use view binding because it came out after the course, but it's a feature to be aware of. View binding replaces findViewById . View binding generates a binding object for each XML layout. You use this binding object to reference views, using their resource ids as the name: // Creating a binding object for the main_activity.xml layout binding = ActivityMainBinding . inflate ( layoutInflater ) // Referencing a view with the ID roll_button binding . rollButton View binding has the following benefits over findViewById : Type safety - findViewById requires you to specify the type of view you expect to be returned. For example, if you accidentally specify that you expect an ImageButton to be returned when the a...
Comments
Post a Comment