What is the simplest way to get thread-safe property in Kotlin? - properties

Is it the simplest way to get thread-safe property of Double type in Kotlin?
class SomeClass {
#Volatile
var someProp : Double? = null
#Synchronized get
#Synchronized set
}

Related

How to get both static and instance access to a classes field

Suppose I have an abstract superclass A. That class has a property abstract val predicate: (ModelClass) -> Boolean.
Let B be a subclass.
I want to be able to do both of the following:
Use the predicate from an instance aInstance.predicate
Also use that predicate elsewhere, without having to create an instance to get that predicate B.predicate
How can I do this.
I don't think this is possible.
There is no such thing as an abstract static method in Kotlin or Java.
Perhaps this will give more insight.
Does your class need to be abstract? Maybe the code below can be useful:
open class MyClass {
companion object myCompanion {
val myStatic = "MyClass"
}
open val myStatic = myCompanion.myStatic
}
class MySubClass : MyClass() {
companion object myCompanionSubClass {
val myStatic = "MySubClass"
}
override var myStatic = myCompanionSubClass.myStatic
}
fun main() {
println(MyClass.myStatic)
val t = MyClass()
println(t.myStatic)
println(MySubClass.myStatic)
val subClass = MySubClass()
println(subClass.myStatic)
}
In this code you can define a static property and use it from the class or any instance. It is also possible to override the property in a subclass and use it in the same way.

Primitive properties for a class in init block - Kotlin

I'm defining a Kotlin class with a number of primitive properties:
class Contract (contractEntity : ContractEntity) EntityDao<ContractEntity> {
var id : Long // <- This is a primitive datatype, needs to be initialized
var concept : String //<- This also needs to be initialized or declared abstract
init{
mapFromEntity(contractEntity)
}
override fun mapFromEntity(entity : ContractEntity){
id = entity.id
concept = entity.concept
}
}
Now, I want those properties to be initialized with the function mapFromEntity() but I'm stuck with the init block because those are not initialized. What could be a good way to achieve what I'm trying?
So far, the best solution suggest to remove the function that maps the class and use the constructor parameter.
class Contract (contractEntity : ContractEntity) EntityDao<ContractEntity> {
var id = contractEntity.id
var concept = contractEntity.concept
}

Difference between member function and property

What is the difference?
val isFinished: Boolean
get() = actor.actions.size == 0
fun isFinished() = actor.actions.size == 0
I have no idea.
I expect them to be the same.
The first statement defines a property and the second statement defines a method.
Assume you define both in a class Foo.
val foo = Foo()
if(foo.isFinished) {} // property
if(foo.isFinished()) {} // method - see () at invocation
Under the hood, both variants call a function.
Class Property
Your first example is a class property:
class MyClass {
val isFinished: Boolean
get() = actor.actions.size == 0
}
This property defines an (optional) explicit get method, often referred to as a "getter". You could also omit the getter and do:
class MyClass {
val isFinished: Boolean = (actor.actions.size == 0) //parens not necessary
}
If omitted, as shown in the last example, the value will rather be a constant value which isn't re-calculated on each access. Both variants serve different use cases but in most cases, the latter will be sufficient.
Regardless, for a client of this class, the property can be accessed like this:
val mc = MyClass()
mc.isFinished
This shows accessing a property in Kotlin.
Class member function
Functions in Kotlin can also define functions, which can be referred to as member functions. Your second example demonstrates this feature:
class MyClass {
fun isFinished() = actor.actions.size == 0
}
Defining a member function is similar to properties with explicit getters but still different for a client, since they have to invoke a function rather than accessing a property:
val mc = MyClass()
mc.isFinished()
Side Note
Note that the shown function utilizes an expression body which is equivalent to the following block body approach:
class MyClass {
fun isFinished() {
return actor.actions.size == 0
}
}
Learn more about functions here.

"same JVM signature" implementing kotlin interface containing getter method

interface MyInterface {
fun getTheString(): String
}
class MyClass(var theString: String) : MyInterface {
...
}
normally when I have a variable in the constructor for a class, it creates a getter and setter for that variable. In MyClass, the methods getTheString() and setTheString(String) exist when not implementing MyInterface.
When MyClass implements MyInterface, I get the error:
Accidental override: The following declarations have the same JVM signature (getTheString()Ljava/lang/String;):
public final fun (): String defined in MyClass
public abstract fun getTheString(): String defined in MyClass
I also have the error: Class 'MyClass' is not abstract and does not implement abstract member public abstract fun getTheString(): String defined in MyInterface.
So I have a few questions:
Why are 2 getter methods getting generated with the same JVM signature when implementing the interface versus one getter method getting generated without implementing the interface?
Why is it complaining I haven't implemented a getTheString() method when this method is automatically generated by kotlin?
How can I get the getter generated by the variable to become the implementation of the method in the interface?
If the interface is indeed in Kotlin, and you can change it, it should be
interface MyInterface {
val theString: String
}
in the first place. Java will still see getTheString(), but it's nicer to both implement and use in Kotlin.
Otherwise a good option is
class MyClass(#set:JvmName("setTheString") var _theString: String) : MyInterface {
override fun getTheString() = _theString
}
Unfortunately, it still has a duplicate getter, and you can't make only the getter private. Or
class MyClass(private var _theString: String) : MyInterface {
override fun getTheString() = _theString
fun setTheString(value: String) {
_theString = value
}
}
Note that if the interface is in Java, getTheString() will be visible to Kotlin as a property.
See issues https://youtrack.jetbrains.com/issue/KT-6653 and https://youtrack.jetbrains.com/issue/KT-19444 on the Kotlin bug tracker.

The best way to override a property as a constant in Kotlin

Kotlin provides support for property overriding. I an wondering what is the best way to override a property as a constant value.
To be more specific, assume that an abstract val property is declared in a superclass or an interface:
// In a superclass or an interface
abstract val num : Int
In its subclass there are at least 2 ways as far as I can think of to override it:
// In its subclass
override val num : Int = 0
or
// In its subclass
override val num : Int get() = 0
Besides the two, I can also do it in the Java way:
// In a superclass or an interface
abstract fun getNum() : Int
// In its subclass
override fun getNum() : Int = 0
What's the difference among the three in terms of memory and generated bytecode? Which one is the best?
Are there even better ways or patterns in Kotlin to declare properties that are to be overridden as constants?
There's functional difference.
Using assignment You initialize the field when object is created:
override val num : Int = 0
This creates an implicit final backing field with value 0, and getter that always returns same value. This is generated bytecode decompiled into java:
private final int num;
public int getNum() {
return this.num;
}
Second declaration is actually a getter override, which is also a valid way to implement property from the interface. This does not create a backing field, so your property can return different values on each call (for example method call):
override val num : Int get() = 0
Decompiled bytecode:
public int getNum() {
return 0;
}