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 {}
-
let(_:Extension method) Invokes the provided
blockand 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 StringDeclaration
Swift
@inlinable @inline(__always) func `let`<T>(_ block: (Self) -> T) -> TParameters
blockThe block to invoke
Return Value
The result of invoking
block -
takeIf(_:Extension method) Invokes the provided
predicate, if it returnstrue, 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 MyStructDeclaration
Swift
@inlinable @inline(__always) func takeIf(_ predicate: (Self) -> Bool) -> Self?Parameters
predicateThe predicate to invoke
Return Value
The receiver or nil
-
takeUnless(_:Extension method) Invokes the provided
predicate, if it returnstrue, 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 MyStructDeclaration
Swift
@inlinable @inline(__always) func takeUnless(_ predicate: (Self) -> Bool) -> Self?Parameters
predicateThe 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 MyStructDeclaration
Swift
@discardableResult @inlinable @inline(__always) func also(_ block: (inout Self) -> Void) -> SelfParameters
blockThe 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 MyClassDeclaration
Swift
@discardableResult @inlinable @inline(__always) func also(_ block: (Self) -> Void) -> SelfParameters
blockThe block to invoke
Return Value
The receiver
View on GitHub
Install in Dash
ScopeFunctions Protocol Reference