ScopeFunctions

public protocol ScopeFunctions

Conformance to this protocol provides implementation of some of the Kotlin Scope Functions. To add conformance to your own types, see examples below.

class MyClass: ScopeFunctions {}

struct MyStruct: ScopeFunctions {}

Public Functions

  • let(_:) Extension method

    Invokes the provided block and returns the result, providing the receiver as the only argument.

    struct MyStruct: ScopeFunctions {}
    
    let retval = MyStruct().let {
       // do stuff with $0 and return a different value
       "my string value"
    }
    
    // do something with retval which is a String
    

    Declaration

    Swift

    @inlinable
    @inline(__always)
    func `let`<T>(_ block: (Self) -> T) -> T

    Parameters

    block

    The block to invoke

    Return Value

    The result of invoking block

  • takeIf(_:) Extension method

    Invokes the provided predicate, if it returns true, returns the receiver, otherwise returns nil.

    struct MyStruct: ScopeFunctions {}
    
    let retval = MyStruct().takeIf {
       // test $0 to determine return value
    }
    
    // do something with retval which is an optional MyStruct
    

    Declaration

    Swift

    @inlinable
    @inline(__always)
    func takeIf(_ predicate: (Self) -> Bool) -> Self?

    Parameters

    predicate

    The predicate to invoke

    Return Value

    The receiver or nil

  • takeUnless(_:) Extension method

    Invokes the provided predicate, if it returns true, returns nil, otherwise returns the receiver.

    struct MyStruct: ScopeFunctions {}
    
    let retval = MyStruct().takeUnless {
       // test $0 to determine return value
    }
    
    // do something with retval which is an optional MyStruct
    

    Declaration

    Swift

    @inlinable
    @inline(__always)
    func takeUnless(_ predicate: (Self) -> Bool) -> Self?

    Parameters

    predicate

    The predicate to invoke

    Return Value

    The receiver or nil

  • also(_:) Extension method

    Invokes the provided block, providing the receiver as the only argument and returns the receiver.

    struct MyStruct: ScopeFunctions {}
    
    let retval = MyStruct().also {
       // configure $0
    }
    
    // do something with the configured retval which is a MyStruct
    

    Declaration

    Swift

    @discardableResult
    @inlinable
    @inline(__always)
    func also(_ block: (inout Self) -> Void) -> Self

    Parameters

    block

    The block to invoke

    Return Value

    The receiver

  • also(_:) Extension method

    Invokes the provided block, providing the receiver as the only argument and returns the receiver.

    struct MyClass: ScopeFunctions {}
    
    let retval = MyClass().also {
       // configure $0
    }
    
    // do something with the configured retval which is a MyClass
    

    Declaration

    Swift

    @discardableResult
    @inlinable
    @inline(__always)
    func also(_ block: (Self) -> Void) -> Self

    Parameters

    block

    The block to invoke

    Return Value

    The receiver