# Object as argument for function

**URL:** <https://discuss.kotlinlang.org/t/object-as-argument-for-function/19612>\
**Category:** Uncategorized\
**Created:** [October 22, 2020, 9:01pm UTC](https://discuss.kotlinlang.org/t/object-as-argument-for-function/19612 "2020-10-22T21:01:44Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)\
**Post date:** [October 23, 2020, 12:16am UTC](https://discuss.kotlinlang.org/t/object-as-argument-for-function/19612/21 "2020-10-23T00:16:38Z")

</div>

> [@absoluterookie](#):
>
> Isn’t there any way to add some variables to an existing class

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](https://kotlinlang.slack.com/). That’s a really helpful place for conversational style learning and quick questions.  
There’s also [Kotlin Playground](https://play.kotlinlang.org/), 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.

---

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 12:18am UTC](https://discuss.kotlinlang.org/t/object-as-argument-for-function/19612/22 "2020-10-23T00:18:46Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)\
**Post date:** [October 23, 2020, 12:38am UTC](https://discuss.kotlinlang.org/t/object-as-argument-for-function/19612/23 "2020-10-23T00:38:23Z")

</div>

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):

```run-kotlin
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.

---

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 12:52am UTC](https://discuss.kotlinlang.org/t/object-as-argument-for-function/19612/24 "2020-10-23T00:52:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chipays](https://avatars.discourse-cdn.com/v4/letter/c/ad7895/32.png) [@chipays](https://discuss.kotlinlang.org/u/chipays)\
**Post date:** [October 23, 2020, 11:21am UTC](https://discuss.kotlinlang.org/t/object-as-argument-for-function/19612/25 "2020-10-23T11:21:08Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [October 23, 2020, 11:45am UTC](https://discuss.kotlinlang.org/t/object-as-argument-for-function/19612/26 "2020-10-23T11:45:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sighoya](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@sighoya](https://discuss.kotlinlang.org/u/sighoya)\
**Post date:** [October 29, 2020, 11:09am UTC](https://discuss.kotlinlang.org/t/object-as-argument-for-function/19612/27 "2020-10-29T11:09:11Z")

</div>

> [@absoluterookie](#):
>
> ```auto
> object myObject{
> val fixedInt:Int = 1
> var changeableString:String="Hello World"
> }
> 
> //PROBLEM
> fun someFun(o:object/*<---Not working*/){
> println(o.changeableString+" "+o.fixedInt)
> o.changeableString="Has Changed"
> println(o.changeableString+" "+o.fixedInt)
> }
> someFun(myObject)
> 
> ```

You can do that:

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

```

And then

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

```

By the law of dispatching:

```kotlin
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.

[Previous page](https://discuss.kotlinlang.org/t/object-as-argument-for-function/19612.md?page=1)
