Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setter overloading in Kotlin

Tags:

setter

kotlin

When trying to define a setter that accepts a parameter type that can be used to construct a property, thusly:

class Buffer(buf: String) {}

class Foo {
    var buffer: Buffer? = null
        set(value: String) {
            field = Buffer(value)
        }
}

I get the error message:

Setter parameter type must be equal to the type of the property

So what's meant to be the Kotlin way of doing this?

like image 981
Basel Shishani Avatar asked Mar 29 '17 06:03

Basel Shishani


People also ask

Is overloading possible in Kotlin?

Kotlin supports overloading for callables and properties, that is, the ability for several callables (functions or function-like properties) or properties with the same name to coexist in the same scope, with the compiler picking the most suitable one when such entity is referenced.

What is setter in Kotlin?

In Kotlin, setter is used to set the value of any variable and getter is used to get the value. Getters and Setters are auto-generated in the code. Let's define a property 'name', in a class, 'Company'. The data type of 'name' is String and we shall initialize it with some default value.

Are getters and setters necessary in Kotlin?

In Kotlin, getters and setters are optional and are auto-generated if you do not create them in your program.

What are Kotlin's properties?

Properties. Properties are the variables (to be more precise, member variables) that are declared inside a class but outside the method. Kotlin properties can be declared either as mutable using the “var” keyword or as immutable using the “val” keyword. By default, all properties and functions in Kotlin are public.

What is operator overloading in Kotlin?

Kotlin Operator Overloading. Since Kotlin provides user-defined types, it also provides the additional functionality to overload the standard operators, so that working with user-defined types is easier. All of the unary, binary, relational operators can be overloaded. The operators are overloaded either through the member functions or through ...

How to use getters and setters in Kotlin?

In Kotlin, setter is used to set the value of any variable and getter is used to get the value. Getters and Setters are auto-generated in the code. Let’s define a property ‘ name ‘, in a class, ‘ Company ‘. The data type of ‘ name ‘ is String and we shall initialize it with some default value. The above code is equivalent to this code:

How to turn a function into an operator in Kotlin?

In order to turn a Kotlin function with a pre-defined name into an operator, we should mark the function with the operator modifier. For example, we can overload the “+” operator: operator fun Point.plus (other: Point) = Point (x + other.x, y + other.y) This way we can add two Points with “+”:

Why Kotlin is better than other programming languages?

Since Kotlin provides user-defined types, it also provides the additional functionality to overload the standard operators, so that working with user-defined types is easier. All of the unary, binary, relational operators can be overloaded.


Video Answer


1 Answers

As of Kotlin 1.1 it is not possible to overload property setters. The feature request is tracked here:

https://youtrack.jetbrains.com/issue/KT-4075

Currently, you would have to define a buffer extension function on String:

val String.buffer : Buffer
    get() = Buffer(this) 

and set the value with

Foo().buffer = "123".buffer
like image 167
Ingo Kegel Avatar answered Sep 30 '22 14:09

Ingo Kegel