IIFE pin type in Swift

IIFE (Instant Call Function Expression) is a very commonly used template in JavaScript. Swift also looks like supproting. Like this:

let one = { 1 }
/// one: () -> Int

and the explicit type declaration and persistent write function works well.

let one:Int = { $0 }(1)
/// one: Int

but the constant-returnd function cannot be deduced by its own type.

let one = { $0 }(1)
/// SourceKitService
///    Terminated
///
/// Editor functionality
/// temporarily limited.

and I force this shell to run.

//inferred.swift

#!/usr/bin/xcrun swift

var i:Int = 0
let id = { $0 }(i)

println(id)

Whemphasized textile silgen closexprpr SIL @ _TF8inferredU_FRSiSi function for expression in [./inferred.swift:4:10 - line: 4: 15] RangeText = "{$ 0}" [1] 29364 segmentation error. /inferred.swift

Is there something I missed or did I have the wrong styntax?

+4
source share
1 answer

, -, Swift " ", .

, :

let one = { 1 }

... . , . , , , :

let one = { 1 }()

:

let one = { $0 }(1)  // one: (Int) = 1

let i = 1            // i: Int = 1
let one = { $0 }(i)  // one: (Int) = 1

- 5, i var let. ( , , SourceKit , , .)


- - . , Xcode Core Data:

lazy var managedObjectContext: NSManagedObjectContext? = {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
    let coordinator = self.persistentStoreCoordinator
    if coordinator == nil {
        return nil
    }
    var managedObjectContext = NSManagedObjectContext()
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
}()

- nil, , , .

+3

All Articles