![]() However, semantically it’s a win.ĭata classes like Mixtape and Podcasts indicate they are a list of items. How is this helpful?Īt a glance, this little technique might not seem to be of much value. ![]() It’s because, with this approach, we can have clean models with logic separated from the data, as we discussed in the clean models article. Wondering why we have used extension functions and properties for our Mixtape instead of having them defined inside the class? While there are plenty of standard operator overloads available, we will focus on getting the access working for our custom class in this article.Ī quick look into the List class of llections shows up an overloaded get(index: Int) like this:įor (index in 0 until mixtape. Kotlin supports operator overloading with the keyword operator. Before we get into the implementation, let’s see how subscripts work for Lists in Kotlin. Now the question is, can we build subscript access for our custom data models? Our code becomes robust in the sense that we can easily swap out arrays with Lists and our code will work. No matter if we have an array or a List, as long as our naming convention represents the property to be a sequence of data, we can apply the same access method. ![]() This improved syntax is handy because we don’t have to remember different method names or access strategies for different data structures.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |