A
Kotlin | C# |
---|---|
abstract Modifier for classes, properties and functions Learn more |
abstract Modifier for classes, methods, properties, indexers, and events Learn more |
actual Platform-specific modules feature. Functions, classes, interfaces, enumerations, properties, and annotations can be declared as expect in common module, and actual in platform-specific implementations. As of 2023 expect/ actual feature is in Beta Learn more |
Kotlin expect is like an interface, it cannot have an implementation, and actual is like an implementation of interface feature (but it can be an interface itself). Update. See @teneko 's comments below to see how to get the same behavior using partial methods and conditional preprocessor directives. |
also() Scope function. Used to avoid if (x == null) construction at any price Learn more |
No short equivalent. Can be defined as extention function that takes lambda and returns the object it extends |
Anko Kotlin library for Android, deprecated as of 2023 Learn more (etymology: ANdroid + KOtlin) |
Xamarin.Forms Xamarin is cross-platform, while Anko is not Learn more |
annotation Annotation class defines custom formats for storing metadata Learn more |
System.Attribute interface Class that implements System.Attribute interface defines custom attributes, i.e. formats for storing metadata Learn more |
Any Root of type hierarchy Learn more |
object Root of type hierarchy Learn more |
apply() Scope function. Used to avoid if (x == null) construction at any price Learn more |
No short equivalent. Can be defined as extention function that takes lambda and returns the object it extends |
as Unsafe cast operator, throws an exception when fails Learn more |
(T)x type cast Throws an exception when fails Learn more |
as? Safe cast operator, returns null if fails to cast Learn more |
as Returns null if fails to cast Learn more |
Top comments (4)
expect
(Kotlin) is similiar toextern
(C#) orpartial
(C#). The idea is to logically seperate method definitions by (possible) conditions (most likely cpu architecture).partial
is acting at compile time andextern
at runtime.@teneko , thank you for your comment! Indeed, Kotlin expect is similar to C# partial and extern methods in their separation of definition and implementation. Meanwhile, there's a big difference. Kotlin expect can have many implementations, while C# partial and extern methods can only have 1 implementation. I don't see any way to use them for different cpu-dependent implementations
Hi again. There are many ways actually. when using partial you have to use MSBuild logic to produce different outputs depending on architecture and platform. You then typically work with zero or more conditional preprocessor directives. When using
extern
, you can use a custom import resolver to differentiate at runtime what library you want to load (.dll or .so, or whatever).Wow, that's impressive! I've updated my post. Thanks!