Home > Blockchain >  Why does return <expr> work when let var = <expr>; return var give an error that it can&
Why does return <expr> work when let var = <expr>; return var give an error that it can&

Time:04-26

I have the following code, which compiles & works fine:

import RealmSwift

class RealmBucket : Object {
    @Persisted var id : UUID
    @Persisted var title : String
    convenience init(_ id: UUID, _ title: String) {
        self.init()
        self.id = id
        self.title = title
    }
}

func loadBuckets() -> [Bucket] {
    let realm = try! Realm()
    let realmBuckets = realm.objects(RealmBucket.self)
    return realmBuckets.map { Bucket(id: $0.id, title: $0.title) }
}

but if I change the loadBuckets() function to:

func loadBuckets() -> [Bucket] {
    let realm = try! Realm()
    let realmBuckets = realm.objects(RealmBucket.self)
    let result = realmBuckets.map { Bucket(id: $0.id, title: $0.title) }
    return result
}

(just the last line changed) I get the error:

Cannot convert return expression of type 'LazyMapSequence<Results<RealmBucket>, Bucket>' to return type '[Bucket]'

If I change the let line to be:

        let result : [Bucket] = realmBuckets.map { Bucket(id: $0.id, title: $0.title) }

then it works again.

I can think of a couple possible explanations for the behavior, but they all seem to point to a compiler bug, or language deficiency, and I'm guessing that perhaps there is some language feature I'm unaware of.

Does anyone know what the root cause of the difference is? I'm relatively new to Swift, and hoping to learn something from this.

CodePudding user response:

You have to explicitly define the return type of map function, when you use shorthand closure syntax, probably that is the reason.

CodePudding user response:

A “return” statement knows the type it has to return, so if applies that knowledge when the right hand side is evaluated, and can often convert that result to the correct type.

A let statement with no type means the compiler doesn’t know which type is needed. So the right hand side can be evaluated differently.

  • Related