![]() ![]() While all this may sound complicated, it's actually a very efficient solution, as it just uses pyOf under the hood in the bytecode. This calls the arrayOf function we usually create arrays with, which takes a vararg parameter, and uses the spread operator which is a way to pass in the elements of an array (which cars is, inside the buy function) as the vararg parameters ( docs here). This can be created using the following syntax: drive(arrayOf(*cars)) Additional attributes of the annotation can be specified by annotating the annotation class with meta-annotations: Target specifies the possible kinds of elements. ![]() To declare an annotation, put the annotation modifier in front of a class: annotation class Fancy. You could also use new String yourList.size () instead of new String 0. So if you wish to keep these function signatures, you'll need to create a new array that contains everything from your read-only array. Annotations are means of attaching metadata to code. Use List.toArray (T arr): yourVarargMethod (yourList.toArray (new String 0)) (Replace String with whatever type of objects your list contains.) Details String. The problem is that when you have a vararg cars: Car function parameter, its type inside the function is Array, which basically represents a read-only array of the arguments the function was called with - after all, it wouldn't really be a nice thing to start modifying the arguments you got as a vararg (function parameters in Kotlin are read-only vals, and this is something very similar).īut your drive function requires a regular Array, which of course is writable as well. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |