Object as argument for function

No. Classes are not key-value stores like JS objects (aka, Map or in Python, dict). There’s several ways (software design patterns) to solve this in a typed language.


Here’s a link to the Kotlin Slack. That’s a really helpful place for conversational style learning and quick questions.
There’s also Kotlin Playground, which lets you edit and run kotlin in your browser–it even gives you auto-complete (ctrl-space). That way you don’t have to have Andriod Studio for testing snippets of code.

2 Likes

Thank you for that
I will try it by combine a class and an object. Should fit my needs

class MyC{
      var MyOb = Object{}
}

than i should be able to do my chances and adds and so on i guess

Nope. Unfortunately not.

Alright. I do the class with all my possible variables and function

I think you are trying to use “object” in a way that JS uses an “object”. It’s the same word but they mean totally different things.

Here’s a class that stores any number of greetings. It adds items in a map (key-value data structure):

class GreetingTranslator {
   val translations: MutableMap<String, String> = mutableMapOf()
}

fun main() {
    val myGreetings = GreetingTranslator()
    myGreetings.translations += Pair("Spanish", "Hola")
    myGreetings.translations += "Italian" to "Salve" // Another way to add a key-value pair. Same thing as Pair("one", "two")
    println(myGreetings.translations)
}

In JS, the idea of an “object” is more like a container or dictionary where you can look up properties and functions with a name. This is very different from how the word “object” is used in other languages. Some of the designs you may be used to from JS likely rely on using a Map and adding/removing from it. You’ll find that in Kotlin you still can do those patterns even though they’ll look slightly different. You’ll also discover that there are alternative designs that you can’t do well in JS that are available in Kotlin.

1 Like

Yep, currently i read about mapping. Thank you

I know that kotlin isn’t javasript at all from the beginning. And i’ve never expected so. But it’s still kind of diapointing, because it’s more work (lines) for the same thing. And actually i’ve expected less lines in a so high language. But that’s why (as i’ve mentioned already) Android App Developers earn 10 times more to javascript developers.

Well, it is as it is

1 Like

This has nothing to do with high and low level languages.

Kotlin is strongly typed static language, while JavaScript is weakly dynamic typed. You will encounter these pains with any other strongly typed static language like Typescript, Java, C#, etc.

Kotlin has a guide for Python developers. While not completely applicable to a JS developer, it should suffice to point out the major differences between JS and Kotlin as well. The article on classes seems relevant here https://kotlinlang.org/docs/tutorials/kotlin-for-py/classes.html

Kotlin’s object model is substantially different from Python’s. Most importantly, classes are not dynamically modifiable at runtime! … All properties (attributes) and functions that might ever be needed on a class must be declared either directly in the class body or as extension functions, so you should think carefully through your class design.

1 Like

Your main confusion seems to do with the difference between “duck typing” versus static typing. A statically typed language like Kotlin is a language that puts emphasis on allowing a compiler to verify the correctness of the code. This is mainly valuable in large projects with many contributors. In those cases it is harder to be consistent and help by the compiler is much more valuable. Especially when taking into account that the key time cost in programming is thinking, not typing.

1 Like

You can do that:

class MyObject{
 val fixedInt:Int;
 var changeableString:String;
}
object MySpecializedObject extends MyObject{
 val fixedInt:Int=1;
 var changeableString:String=". item";
}

And then

fun(myObject:MyObject)=myObject.fixedInt.toString()+myObject.changeAbleString()

By the law of dispatching:

fun(MySpecializedObject)=="1. item"

but what you wrote expects either duck typing, e.g. an Object is maybe an MySpecializedObject and structural types, e.g. MyObject={fixedInt:Int,changeableString:String}, both of which aren’t supported in Kotlin.

The former will never be supported because Kotlin is statically typed, maybe some modifier like duck could be added in future.
The latter is more interesting but would probably cause some problems on the JVM or hardening Java compatibility, don’t know.

1 Like