Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an Kotlin equivalent `with` function in Swift?

Tags:

swift

kotlin

In Kotlin, we could change the below

// Original code
var commonObj = ClassCommonObj()
 commonObj.data1 = dataA
 commonObj.data2 = dataB
 commonObj.data3 = dataC

// Improved code
var commonObj = ClassCommonObj()
with(commonObj) {
    data1 = dataA
    data2 = dataB
    data3 = dataC
}

However in Swift as below, do I have equivalent with function to use?

// Original code
var commonObj = ClassCommonObj()
 commonObj.data1 = dataA
 commonObj.data2 = dataB
 commonObj.data3 = dataC
like image 603
Elye Avatar asked Dec 01 '17 04:12

Elye


People also ask

Can you convert Swift to Kotlin?

Gryphon is a command line application that can translate Swift code into Kotlin code. It was especially designed to enable app developers to translate platform-independent parts of their iOS apps into code they can use in their Android apps.

Is Kotlin similar to Swift?

Similar to Kotlin, Swift is a general-purpose, compiled programming language. It integrates with Objective-C code. In fact, it arguably improves on Objective-C through extra flexibility, concise syntax and ease of use.

How different is Kotlin from Swift?

In terms of development, Kotlin is used as a programming language for Android whereas Swift is used as a programming language for iOS app development. Both of these languages are insanely popular due to their ease of use, overall user experience, and efficiency in coding.

What is Swift Kotlin?

For developing mobile applications, the most commonly used programming languages are Kotlin and Swift. While Kotlin is used for developing Android mobile applications, Swift is used for developing iOS mobile applications. Both these programming languages have their own set of features, which make them stand out.


1 Answers

Unfortunately, no such functionality so far in Swift. However, similar functionality can be reached with the power of extensions:

protocol ScopeFunc {}
extension ScopeFunc {
    @inline(__always) func apply(block: (Self) -> ()) -> Self {
        block(self)
        return self
    }
    @inline(__always) func with<R>(block: (Self) -> R) -> R {
        return block(self)
    }
}

This protocol and extension provides two inline functions, where one can be served to return processed object, and the other is strictly similar to with in Kotlin and other languages (Visual Basic supported in 90s).

Usage

Specify types which these functions should apply to:

extension NSObject: ScopeFunc {} 

apply:

let imageView = UIImageView().apply {
    $0.contentMode = .scaleAspectFit
    $0.isOpaque = true
}

Here we create an object and once the closure is executed, modified object is returned.

with:

imageView.with {
    $0.isHidden = true
}

Works equal to with in Kotlin.

Originaly based on this source code.

NOTE:

Swift compiler is generally regarded as smart enough to decide whether or not a function should be inlined. Quite likely, these two would be inlined due to their relative compactness even without strictly specifying @inline (__always). Either way, you should know that this keyword does not affect the logic and the result of these, because inlining is about optimizing the program.

like image 68
Hexfire Avatar answered Nov 22 '22 17:11

Hexfire