D
D
driverx182019-06-10 22:37:13
Swift
driverx18, 2019-06-10 22:37:13

What is the difference between this entry with and without a generic?

I found a lesson on generics, the author shows an example where they can be used. Quoting an example:

func printEvery<T>(_ array: [T]) {
    for element in array {
        print(element)
    }
}

To bypass an array of any type and output any element instead of supposedly not making functions: printInts, printStrings (due to strong typing).
And all this works, but what prevents us from making the same function without a generic, but with the Any type?
func printEvery(_ array: [Any]) {
    for element in array {
        print(element)
    }
}

This example also works.
I'm new to Swift, so this might sound like a dumb question.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
doublench21, 2019-06-11
@driverx18

The two types Anyand AnyObjectare the so-called Nonspecific types . Non-specific types serve as a pointer to the compiler that any type can be stored. BUT you have to pay for this feature and you pay:
Firstly , due to the lack of type information at the time of compilation, you cannot call, say, some property String, even knowing that the assigned variable is actually type String. You simply won't have that kind of information exactly until you convert that type back.
Of course, at runtime, Swift understands exactly what type is in the type variable Any, but this is only at runtime, there is no information about this at the time of compilation.
Secondly , you pay with speed when working with such types. The compiler has no type information, which means it cannot perform the necessary optimizations.
The function print(_:separator:terminator:)receives information about what to output to the console, based on the fact that all standard types implement protocols CustomStringConvertible, CustomDebugStringConvertible,CustomReflectablethe implementation of which contains information about the type (text). If the type does not have an implementation of these protocols, print(_:separator:terminator:)outputs just the name of the type.
Moreover, you can always output the so-called dump of any absolute type to the console using the dump(_:name:indent:maxDepth:maxItems:).
For more detailed information about all the possibilities and varieties of functions for working with Debugging and Reflection , you can look here
-------------------------------------------------- -------------------------------------------------- --------------------
Generics are just placeholders for types. At the time of compilation, the compiler instead of your substitute, let's say, will Tsubstitute a specific type, say String. Which in turn leads to the fact that generics do not have the problems mentioned above. (Even my "first" is missing from generics if you put restrictions on them.)
------------------------------ -------------------------------------------------- ----------------------------------------
PS In fact Generics are one of the most beautiful and Strengths of the Swift language, and my pathetic attempt to explain to you - does not properly explain anything. Study them.

I
Ivan Vorobei, 2019-06-11
@ivanvorobei

But what prevents us from making the same function without a generic, but with the Any type?

Nothing . You can also use forEach. I'm not saying that generics are not needed, just answered the question.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question