BigZaphod
(Sean Heber)
1
Take this example code which is accepted without comment from the compiler:
enum SampleEnum {
case anOption
func process() {
switch self {
case .anOption: anOption()
}
}
fileprivate func anOption() {
// stuff
}
}
However moving the anOption() function out of the enum type results in an error:
enum SampleEnum {
case anOption
func process() {
switch self {
case .anOption: anOption() // Enum case 'anOption' cannot be used as an instance member
}
}
}
fileprivate func anOption() {
// stuff
}
I'm curious if this is intentional behavior here or a bug of some kind? (Feels like a bug to me, tbh.)
SDGGiesbrecht
(Jeremy David Giesbrecht)
2
I don’t know if it is intentional, but the compiler always finds static members first and prefers them over the global scope, resulting in errors.
var property = 0
struct Structure {
static var property = 0
func useProperty() {
property = 1 // Static member 'property' cannot be used on instance of type 'Structure'
}
}
What you can do is tell it where to look explicitly:
MyType.property = 1
MyModule.property = 1
1 Like
I wouldn't say it's a bug. Here's another example:
struct Foo {
static let bar: Foo = .init()
func doWork() {
bar() // error
}
}
func bar() {}
1 Like