Confused by the example code which adds new instance methods to types

SLN

The following code snippet is copied from the official document where it is used to demonstrate how to use the Extensions to add method to existing types.

extension Int {
    func repetitions(task: () -> Void) {
        for _ in 0..<self {
            task()//what's going on here?
        }
    }
}

3.repetitions { //why I don't have to call as `repetitions()`
    print("Hello!")
}

Question: The question is not asking how the extinsion extens. I just feel a bit confused about this code, why it looks like so? why use task() inside function body? Where it comes from? for the line 3.repetition why not write it as 3.repetition()

Thanks a lot

matt

repetitions is a function that accepts as its parameter a function and calls that function several times.

To call repetitions, we could say (note, this is Swift 3):

func sayHi() {
    print("Hello")
}
3.repetitions(task:sayHi)

But why define an extra name sayHi? Instead we use an anonymous function:

3.repetitions(task:{print("Hello"})

But in that case we are allowed to omit the parenthesis in this call to repetitions and use trailing syntax:

3.repetitions{print("Hello")}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Confused about Instance variables, creating new objects and methods?

From Dev

Confused by the rpcgen example code

From Dev

Which is better? Use static methods or create new instance each time?

From Dev

Confused about the ways to write code for instance variables

From Dev

Naming convention for methods which clear instance variable

From Dev

Does calling @Bean methods create a new instance of the object or the bean instance?

From Dev

ruby - Enumerable example - confused

From Dev

Does Dart create new methods for each new instance?

From Dev

Confused with recursive methods & loops

From Dev

Confused about instance initialization

From Dev

Marking methods which do not read/write instance state

From Dev

Marking methods which do not read/write instance state

From Dev

Does List<T> adds new objects by referencing or by creating a new instance of added object for its own use?

From Dev

Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true

From Dev

Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true

From Dev

Why module included outside the class adds instance methods to class's objects

From Dev

How to use new_record?, changed? and persisted? methods in rails in this example

From Dev

How to create TypeScript definition file for external javascript which adds methods to String

From Dev

I am confused about constant, constant references, references to constants, ... (including example code)

From Dev

Creating a new instance of an inherited class through the inherited static methods

From Dev

Which extension adds jquery?

From Dev

New Methods: Implementing INFINITE LOOP which can be STOPPED upon request

From Dev

How to use methods which are not in interface with interface i = new class();

From Dev

How to treat methods which are only called by other methods from the same class but shouldn't be called by an instance?

From Dev

Cannot form new instance of a function which returns itself

From Dev

I am confused in two ways to create one to many relationship in code first migration . Which one to choose?

From Dev

Make new column which adds 30 days to date on every row R

From Dev

JPanel GridLayout (filled partially by loop) adds new values to end regardless of order in which they were added

From Dev

Make new column which adds 30 days to date on every row R

Related Related

  1. 1

    Confused about Instance variables, creating new objects and methods?

  2. 2

    Confused by the rpcgen example code

  3. 3

    Which is better? Use static methods or create new instance each time?

  4. 4

    Confused about the ways to write code for instance variables

  5. 5

    Naming convention for methods which clear instance variable

  6. 6

    Does calling @Bean methods create a new instance of the object or the bean instance?

  7. 7

    ruby - Enumerable example - confused

  8. 8

    Does Dart create new methods for each new instance?

  9. 9

    Confused with recursive methods & loops

  10. 10

    Confused about instance initialization

  11. 11

    Marking methods which do not read/write instance state

  12. 12

    Marking methods which do not read/write instance state

  13. 13

    Does List<T> adds new objects by referencing or by creating a new instance of added object for its own use?

  14. 14

    Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true

  15. 15

    Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true

  16. 16

    Why module included outside the class adds instance methods to class's objects

  17. 17

    How to use new_record?, changed? and persisted? methods in rails in this example

  18. 18

    How to create TypeScript definition file for external javascript which adds methods to String

  19. 19

    I am confused about constant, constant references, references to constants, ... (including example code)

  20. 20

    Creating a new instance of an inherited class through the inherited static methods

  21. 21

    Which extension adds jquery?

  22. 22

    New Methods: Implementing INFINITE LOOP which can be STOPPED upon request

  23. 23

    How to use methods which are not in interface with interface i = new class();

  24. 24

    How to treat methods which are only called by other methods from the same class but shouldn't be called by an instance?

  25. 25

    Cannot form new instance of a function which returns itself

  26. 26

    I am confused in two ways to create one to many relationship in code first migration . Which one to choose?

  27. 27

    Make new column which adds 30 days to date on every row R

  28. 28

    JPanel GridLayout (filled partially by loop) adds new values to end regardless of order in which they were added

  29. 29

    Make new column which adds 30 days to date on every row R

HotTag

Archive