Skip to content

Switch expression results cannot be assigned to self. #66489

Closed as duplicate of#66412
@JessyCatterwaul

Description

@JessyCatterwaul

Compiles:

func ƒ<Value>(value: Value) -> Value {
  switch value {
  case let value: value
  }
}

Does not compile:

extension Bool { // Type doesn't matter, though `Never` will crash the compiler.
  init(value: Self) {
    self = switch value {
    case let value: value // This will compile with a warning, but assign the argument, not the bound value.
    case let v: v // Cannot find 'v' in scope
    }
  }
}

Wrapping in a closure is a solution:

extension Bool {
  init(value: Self) {
    self = { switch value {
    case let value: value
    } } ()
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    assignmentsFeature → expressions: assignmentsbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfexpressionsFeature: expressionsswift 5.9switchFeature → statements: 'switch' statementstype checkerArea → compiler: Semantic analysisunexpected errorBug: Unexpected error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions