Swift: case foo (let bar): no type or job?

In Swift, I understand that "let" defines a constant. No problems. Therefore, "let foo = 42" and "let foo: Int" make sense. But I see several cases where just “let foo” is written without an assignment or type specification. For example, "case bar (let foo): ..."

What exactly happens when "let foo" itself is in such code?

+4
source share
1 answer

This notation is used to bind an associated enumeration value.

Take this for example:

let anOptionalInt: Int? = 15

switch (anOptionalInt) {
case .Some(let wrappedValue):
    print(wrappedValue)

case .None:
    print("the optional is nil")
}

This works because it Optionalis an enumeration. The first expression can be written as:

let anOptionalInt: Optional<Int> = Optional.Some(15)

: .Some .None. .Some , Wrapped, .None .

, Optional.None nil.

+3

Source: https://habr.com/ru/post/1648939/


All Articles