Avoid using `dynamic` and `@inline(__always)` togetherSW-W1026
The dynamic
keyword is used to enable dynamic dispatch for a method, allowing the method to be overridden at runtime. On the other hand, the @inline(__always)
attribute is used to force inlining of a method bypassing dynamic dispatch. Using both together can result in inconsistent behavior, as the method may or may not be inlined depending on the compiler optimizations.
The @inline(__always)
attribute is often used to improve performance by eliminating the overhead of method dispatch. However, when used together with dynamic
, it can negate the benefits of inlining, as the method still needs to be dynamically dispatched at runtime. This can result in unnecessary performance overhead.
Using both dynamic
and @inline(__always)
together can make the code harder to understand and maintain. It also introduces an ambiguity in the behavior of the method and can confuse other developers working on
the codebase.
Bad Practice
class C {
@inline(__always) dynamic func f() {}
}
Recommended
class C {
dynamic func f() {}
}
// or
class D {
@inline(__always) func f() {}
}