https://kotlinlang.org logo
#getting-started
Title
# getting-started
k

Kirill Grouchnikov

11/16/2023, 2:00 AM
Take a look at extension properties and functions
🧵 2
e

Everett Corvid

11/16/2023, 6:12 PM
Holy crap thank you! This not only lets me accomplish the goals of rust attributes, but it also solves a problem I was having in C++! I was about to get into a fist fight with some people because they said there is no reason to need to be able to "redefine" a class, I.E. add functions or member variables/parameters to a class after the header, but the thing is you can't define a member struct, AND create a member variable using that struct in the same class. This also means you can't implement certain things on objects that are members of a class; I.E. if you create a struct in a class, you cant use that struct as the key for a map variable within that class, you have to make the struct an awkward vestigial global struct, or create a namespace just for the sake of defining that singular struct outside of the class but not in a global scope. The reason being is, you need to define hashing for the struct in order for it to be used as a map key, and you cant create a map variable in a header file before you define hash, and because hash is apart of std, you cant define it within the class header or definition. There are ways around it, but none of them are idiomatic, and all of them are ugly and hard to read and hard to understand the intent of for no reason, for this absurd strict adherence to order of declaration, NOT definition - in kotlin we have lateinit and these extensions to redefine or add onto classes after theyve been declared and defined, it's great.
k

Klitos Kyriacou

11/17/2023, 10:29 AM
I'm not familiar with rust yet, but note that in Kotlin, extension functions don't actually "extend" the class. They don't magically become member functions. They are merely syntactic sugar. They play no part in polymorphism.