Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
743 views
in Technique[技术] by (71.8m points)

optimization - Is there a way to declare an inline function in Swift?

I'm very new to the Swift language.

I wanted to declare an inline function just like in C++ so my func declaration looks like this:

func MyFunction(param: Int) -> Int {
    ...
}

and I want to do something like this:

inline func MyFunction(param: Int) -> Int {
    ...
}

I tried to search on the web but I didn't find anything relevant maybe there is no inline keyword but maybe there is another way to inline the function in Swift.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Swift 1.2 will include the @inline attribute, with never and __always as parameters. For more info, see here.

As stated before, you rarely need to declare a function explicitly as @inline(__always) because Swift is fairly smart as to when to inline a function. Not having a function inlined, however, can be necessary in some code.

Edit: Swift 5 added the @inlinable attribute. Make sure you read up about it, as there may be a couple of gotchas that might make in unusable. It's also only for functions/methods declared public, so it's meant for those wanting to expose inline stuff for those that link to your library.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...