Compile error with `let` in Swift switch statements -


in swift, can slice of array using range operator, this:

let list: string[] = ["first", "middle", "last"] let cdr = list[1..list.endindex] assert(cdr == ["middle", "last"]) 

i'm trying same thing in recursive function takes string[] parameter, not having luck:

func last(xs: string[]) -> string? {     switch xs {     case let (singleitemlist) singleitemlist.endindex == 1:         return singleitemlist[0]     case let(multiitemlist) multiitemlist.endindex > 1:         let cdr: string[] = multiitemlist[1..multiitemlist.endindex] // compilation error!         return last(cdr)     default:         return nil // empty list     } }   last(["first", "middle", "last"]) last(["last"]) last([]) 

the middle case statement doesn't compile. fails error:

playground execution failed: error: <repl>:14:29: error: not find overload 'subscript' accepts supplied arguments         let cdr: string[] = multiitemlist[1..multiitemlist.endindex] // compilation error!                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

am doing wrong? type of value let somehow not string[]?

i'm trying come equivalent recursive last method 1 scala (yes, understand there easier ways last element built-in swift , scala):

def last[t](xs: list[t]): t = xs match {     case list() => throw new error("last of empty list")     case list(x) => x     case y :: ys => last(ys) } 

multiitemlist[1..multiitemlist.endindex] not array. have convert array first. change line following:

let cdr: string[] = array(multiitemlist[1..multiitemlist.endindex]) 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -