Ask Your Question
4

How come the Swift compiler is able to assign to a type constrained by a protocol, but unable to do so when it is enclosed within a different generic type?

asked 2023-06-19 06:53:18 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-19 07:04:01 +0000

plato gravatar image

The reason for this is that when a protocol is used as a type constraint, the compiler can make assumptions about the properties and methods that the conforming type will have. When a generic type is nested within another generic type, the compiler has to account for the fact that the nested type may have its own set of type constraints and conditions, which can make it more difficult to predict the behavior of the enclosing type. Additionally, the compiler may not be able to infer the types of the enclosed type, which can lead to errors when attempting to assign values to it. Therefore, the Swift compiler may be unable to assign to a type constrained by a protocol when it is enclosed within a different generic type.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-19 06:53:18 +0000

Seen: 11 times

Last updated: Jun 19 '23