Closure "shadowing" functions


(Wallacy) #1

class iClass{
    let myProperty:(String)->() = { value in
        print("1: \(value)");
    }
    func myProperty(value: String)->() {
        print("2: \(value)");
    }
}

iClass().myProperty("a"); // 1: a

No error is thrown in this case, however it is not possible to call the
"myProperty (value: String) -> ()" function, is there any alternative? This
is expected behavior?


(Jens Alfke) #2

I’m surprised Swift even allows you to declare a property and a method with the same name.
Why don’t you just rename one of them?

—Jens

···

On Jan 8, 2016, at 4:24 AM, Wallacy via swift-users <swift-users@swift.org> wrote:

class iClass{
    let myProperty:(String)->() = { value in
        print("1: \(value)");
    }
    func myProperty(value: String)->() {
        print("2: \(value)");
    }
}


(Chris Lattner) #3

Agreed, this seems like a bug in the redeclaration checking logic.

-Chris

···

On Jan 8, 2016, at 9:08 AM, Jens Alfke via swift-users <swift-users@swift.org> wrote:

On Jan 8, 2016, at 4:24 AM, Wallacy via swift-users <swift-users@swift.org> wrote:

class iClass{
   let myProperty:(String)->() = { value in
       print("1: \(value)");
   }
   func myProperty(value: String)->() {
       print("2: \(value)");
   }
}

I’m surprised Swift even allows you to declare a property and a method with the same name.


(Tino) #4

iClass.myProperty(iClass())("func")

But I would rather avoid shadowing at all.

···

Am 08.01.2016 um 13:24 schrieb Wallacy via swift-users <swift-users@swift.org>:

class iClass{
    let myProperty:(String)->() = { value in
        print("1: \(value)");
    }
    func myProperty(value: String)->() {
        print("2: \(value)");
    }
}

iClass().myProperty("a"); // 1: a

No error is thrown in this case, however it is not possible to call the "myProperty (value: String) -> ()" function, is there any alternative? This is expected behavior?


(Wallacy) #5

File a bug on bugs.swift.org is enough?

···

Em sex, 8 de jan de 2016 às 17:08, Chris Lattner <clattner@apple.com> escreveu:

> On Jan 8, 2016, at 9:08 AM, Jens Alfke via swift-users < > swift-users@swift.org> wrote:
>
>
>> On Jan 8, 2016, at 4:24 AM, Wallacy via swift-users < > swift-users@swift.org> wrote:
>>
>> class iClass{
>> let myProperty:(String)->() = { value in
>> print("1: \(value)");
>> }
>> func myProperty(value: String)->() {
>> print("2: \(value)");
>> }
>> }
>
> I’m surprised Swift even allows you to declare a property and a method
with the same name.

Agreed, this seems like a bug in the redeclaration checking logic.

-Chris


(Chris Lattner) #6

File a bug on bugs.swift.org <http://bugs.swift.org/> is enough?

That would be great, thanks!

-Chris

···

On Jan 8, 2016, at 11:28 AM, Wallacy <wallacyf@gmail.com> wrote:

Em sex, 8 de jan de 2016 às 17:08, Chris Lattner <clattner@apple.com <mailto:clattner@apple.com>> escreveu:

> On Jan 8, 2016, at 9:08 AM, Jens Alfke via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
>
>
>> On Jan 8, 2016, at 4:24 AM, Wallacy via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
>>
>> class iClass{
>> let myProperty:(String)->() = { value in
>> print("1: \(value)");
>> }
>> func myProperty(value: String)->() {
>> print("2: \(value)");
>> }
>> }
>
> I’m surprised Swift even allows you to declare a property and a method with the same name.

Agreed, this seems like a bug in the redeclaration checking logic.

-Chris