Friday 15 April 2011

ios - Difference between implicit and explicit return from single-expression Closure in Swift -



ios - Difference between implicit and explicit return from single-expression Closure in Swift -

i encountered unusual problem swift compiler.

this code failed compile:

let str = "012345,abc,officer" allow components = split(str, { homecoming $0 == "," }) // ^ error: 'nsstring' not subtype of 'string'

while (without return) compiles , works expected:

let components = split(str, { $0 == "," }) // -> ["012345", "abc", "officer"]

i don't understand why first 1 failed. understanding the doc, { look } syntax sugar of { homecoming look }.

is kind of bug, or missing something?

if create own func split, works regardless of return.

func mysplit(seq: string, isseparator: ((character) -> booleantype)) -> [string]{ var ret:[string] = [] var startidx = seq.startindex; var idx = seq.startindex; idx < seq.endindex; idx = idx.successor() { if isseparator(seq[idx]) { ret.append(seq[startidx ..< idx]) startidx = idx.successor() } } if(startidx <= seq.endindex) { ret.append(seq[startidx ..< seq.endindex]) } homecoming ret; } allow result1 = mysplit(str, { $0 == "," }) allow result2 = mysplit(str, { homecoming $0 == "," })

edit: found of these work.

split(str, { (chr:character) -> bool in homecoming chr == "," }) split(str, { (chr:character) -> bool in chr == "," }) split(str, { chr -> bool in homecoming chr == "," }) split(str, { chr -> bool in chr == "," }) split(str, { (chr:character) in chr == "," }) split(str, { chr in chr == "," }) split(str, { $0 == "," })

but these fail:

split(str, { (chr:character) in homecoming chr == "," }) split(str, { chr in homecoming chr == "," }) split(str, { homecoming $0 == "," })

edit2:

making mysplit reproduces problem.

func mysplit<r:booleantype>(seq: string, isseparator:(character) -> r) -> [string] {

ios swift swift-playground

No comments:

Post a Comment