Translation Instructions:

Parameters and Arguments: An Easy Way to Remember the Difference

Original address:Typealias.com/guides/para…

Original author:Dave Leeds

Brief description (commonly known as bickering):

I recently delve into the generics syntax in Kotlin and find that it is very different from generics in Java, and that Kotlin’s generics are syntactically safer to use than those in Java. But it turns out that Kotlin generics are suddenly getting a lot of terminology, covariant, contravariant, invariant, star projection; Arguments, type parameters, type arguments, and classes, types, subclasses, subtypes, and supertypes are all confusing. So I thought I’d write a series of blog posts about generics in Kotlin to document my frustrations and hopefully help.

Here are some of my previous posts on Kotlin, if you are interested:

Translation series:

  • Should Kotlin define functions or attributes?
  • How to remove all of them from your Kotlin code! (Non-empty assertion)
  • Master Kotlin’s standard library functions: run, with, let, also, and apply
  • All you need to know about Kotlin type aliases
  • Should Sequences or Lists be used in Kotlin?
  • Kotlin’s turtle (List) rabbit (Sequence) race
  • The Effective Kotlin series considers using static factory methods instead of constructors (1)
  • Consider using a builder when encountering multiple constructor parameters in the Effective Kotlin series.

Original series:

  • Everything you need to know about the Kotlin property broker
  • Source code parsing for Kotlin Sequences
  • Complete analysis of Sets and functional apis in Kotlin – Part 1
  • Complete parsing of lambdas compiled into bytecode in Kotlin syntax
  • On the complete resolution of Lambda expressions in Kotlin’s Grammar
  • On extension functions in Kotlin’s Grammar
  • A brief introduction to Kotlin’s Grammar article on top-level functions, infix calls, and destruct declarations
  • How to Make functions call Better
  • On variables and Constants in Kotlin’s Grammar
  • Elementary Grammar in Kotlin’s Grammar Essay

Actual combat series:

  • Use Kotlin to compress images with ImageSlimming.
  • Use Kotlin to create a picture compression plugin.
  • Use Kotlin to compress images.
  • Simple application of custom View picture fillet in Kotlin practice article

Today is a simple blog translation as a prelude to Kotlin’s series on generic syntax:

Let’s get down to business.

If you’ve ever had a hard time remembering the difference between parameters and arguments, this article is tailored for you. Clarifying the differences will help you understand functions better, and even more so generics.

Formal and actual parameters

  • It’s defined as formal parameters.
  • The actual parameters are defined outside

The easiest and easiest way to remember the difference between the two is to associate the word “arugment” with the word “outside.” Remember this sentence

“Take your argument outside!”

Here’s a cartoon I drew to help you remember it:

For more explanation, let’s look at examples of functions and generic classes.

The form in the function takes arguments

This is a simple function that squares integers. What data will be passed? We just want the number squared.

fun square(number: Int): Int {
    return number * number
}
Copy the code

In this function definition, we say number is a formal parameter.

Now that our function is defined, we pass some data to it when we call it.

val radius = 5
val area = Math.PI * square(radius)
Copy the code

Here, outside of the function definition, we say radius is the actual argument to the square function.

A shape in a generic takes arguments

A generic class is a class that has one or more types. For example, here is a very simple Box class that contains only a few other objects.

class Box<T>(var item: T)
Copy the code

Here, in the Box class definition, we say that T is a type parameter.

Using this class is quite simple, we just need to call its constructor and pass in some data of the correct type.

val box = Box<String>("Hello")
Copy the code

Here, outside the Box class definition, we construct it using a String argument.

In fact, Kotlin does some smart type inference, so we don’t even have to specify it explicitly:

val box = Box("Hello")
Copy the code

In this case, it still has a type argument of type String. It is simply implicit in the type of the actual argument to “Hello” that we pass to the constructor.

conclusion

Again, in the case of both functions and generic classes/interfaces, the conclusion is:

  • Parameter – in the definition
  • Argument – outside the definition

Readers have something to say

The purpose of this article is simply to help understand the difference between generic parameters and arguments in Kotlin. Because both terms will be mentioned a lot in future generics articles. So if you don’t understand these nouns and concepts in advance, it’s going to be very difficult to understand some of the deeper things. Let me add a few points:

  • First: To test the theory, let’s analyze the following two examples:
class StringList: List<String>{... }Copy the code

The StringList class implements the List interface and provides a specific type argument: String. As you can see, String is clearly outside the List interface definition, so it is a type argument

class ArrayList<T> :List<T>{... }Copy the code

How w do you analyze this example? In fact, it’s very simple to grasp the point as the authors of this article say: are type parameters inside or outside the class or interface definition, with the inside being the parameter and the outside being the argument

So it’s easy to see that T is a type parameter to the List interface, but it’s also a type parameter to the ArrayList class. The ArrayList class defines its own type parameter T and specifies it as an argument of the parent class (the List interface) type. In Kotlin’s generics, it is stated that if a class inherits a generic class (or implements a generic interface), it must specify a generic argument for the underlying type’s generic parameter. It can be a concrete type or another type parameter. So what you can see is that the T in an ArrayList is actually different from the T in a List, the difference between type parameters and type arguments.

  • Second, it is easy for beginners to fall into the trap of thinking that non-specific types such as T, K, and V are type parameters, while specific meaningful types such as String and Int are type arguments. This kind of understanding is wrong, in short, can be understood as the author of the original text.
  • Third, Kotlin specifies that all generic arguments need to be displayed as declarations that can either be intelligently deduced by the compiler. That is, you can’t just specify a List(Java native ecological type), as in Java, without specifying generic arguments, which Kotlin does not allow, and the compiler will report an error. So I have to praise Kotlin, its grammar is very rigorous and clear, can not be vague, in the definition and declaration must be clear down.
  • Fourth, generic functions also have their own type parameters, which are replaced with type arguments each time the function is called, like parameters and arguments in functions.

This is a simple introduction to a series of articles on generics that will continue to cover Kotlin generics, such as generic erasing and implementing type parameters, generic covariant, contravariant, and star projection. Welcome to continue to follow ~~

Welcome to the Kotlin Developer Association, where the latest Kotlin technical articles are published, and a weekly Kotlin foreign technical article is translated from time to time. If you like Kotlin, welcome to join us ~~~