Answer the question
In order to leave comments, you need to log in
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)
}
}
func printEvery(_ array: [Any]) {
for element in array {
print(element)
}
}
Answer the question
In order to leave comments, you need to log in
The two types Any
and AnyObject
are 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,
CustomReflectable
the 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 T
substitute 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.
But what prevents us from making the same function without a generic, but with the Any type?
forEach
. I'm not saying that generics are not needed, just answered the question.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question