syntax suggestion

Dear Sir/madam,I have a fuction:

import Foundation

public func data(with string:String?) -> Data? {
    if let string = string {
        return string.data(using: String.Encoding.utf8)
    }
    return nil
}

When I use it like:

let data = data(with: "Hello")

There is a compile error: variable used within it's own initial value.

If write like:

let stringData = data(with: "Hello")

Everything is OK.

So I wonder why not support the syntax like:

let data = data(with: "Hello")

Yours sincerely
Rangding Zhang

Hi, Rangding. This is a known issue, tracked by SR-1846 <https://bugs.swift.org/browse/SR-1846&gt;\. Essentially the compiler is not smart enough to know that you mean the existing function 'data' rather than the variable you are defining.

Best,
Jordan

···

On Oct 27, 2016, at 19:06, 张让定 via swift-users <swift-users@swift.org> wrote:

Dear Sir/madam,
I have a fuction:

import Foundation

public func data(with string:String?) -> Data? {
    if let string = string {
        return string.data(using: String.Encoding.utf8)
    }
    return nil
}
When I use it like:

let data = data(with: "Hello")
There is a compile error: variable used within it's own initial value.

If write like:

let stringData = data(with: "Hello")
Everything is OK.

So I wonder why not support the syntax like:

let data = data(with: "Hello")

Yours sincerely
Rangding Zhang

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users