Unexpected error when removing return from single statement getter

Here is a stripped down example of a section of my code base.

struct S {
    var set = [0]

    var x: Int? {
        return set.first
    }
}

I would like to standardize by removing return from single-statement functions and getters, and I found that the code above fails to compile when return is removed.

error: AV.playground:8:12: error: expected '{' to start setter definition
        set.first

This is obviously an interaction between the parser and my choice of variable name. The token set is expected to begin a setter, and is not recognized as a variable name.

The workaround is to use backticks.

struct S {
    var set = [0]

    var x: Int? {
        `set`.first //< Use backticks here.
    }
}
1 Like

As long this a diagnosed error message and not a compiler crash I‘d say it‘s okay because we have the set accessor which is inside the curly brackets. The compiler however should treat your set as an identifier from the given context, but doesn‘t. I would suggest to file a bug report for an improvement of context awareness of the compiler.

Other than that, I think self.set.first should also do the trick here.

Terms of Service

Privacy Policy

Cookie Policy