Avatar ·

What does the `by` keyword do in Kotlin and how to use it for delegation?

Hi! I started learning Kotlin after Java and keep coming across the `by` keyword, but I can't quite grasp its essence. I see it in different contexts: in class declarations, in properties, in constructs with `lazy`. For example, I've seen code like this: ```kotlin class MyClass : BaseInterface by SomeImplementation() ``` Or: ```kotlin val myValue: String by lazy { "Hello" } ``` What does `by` do? Is it some kind of operator? There's nothing like it in Java. I tried reading the documentation, but it's too complicated for a beginner. Can you explain in simple terms how `by` works? When should it be used, and when is it better to avoid it? And why is it called "delegation"? I'd appreciate real-life examples so I can understand how to apply it in my own projects. Thanks in advance!