how to call a (samename) method declared outside with(){} scope in Kotlin - kotlin

I want to call the dada() declared outside with()
There's a line below that supposed to do what I need but doesn't compile.
data class Person(val name: String, val age: Int)
fun main(args: Array<String>) {
var muchos = "muchos"
fun dada(){
println("dada")}
var someRun = Runnable { println(muchos) }
with(someRun){
fun dada(){
println("dodo")}
run()
muchos = "holas"
//*********************//DOES'T COMPILE *******************
this#OuterClass.dada() //DOES'T COMPILE *******************
run()
}
}

Kotlin does not provide a syntax for disambiguating between multiple local functions with the same name. The OuterClass in the this#OuterClass syntax is the name of a specific class that was used in the example where you copied this line from; it does not have a generic meaning of "outer scope".
To make it possible to call both functions, simply give them different names.

Related

IntelliJ - cannot run Kotlin Class in mixed (Java/Kotlin) mode

I am constantly getting the error (bottom right), but I have configured everything to run Kotlin/Java together. So, where is the missing bit which I cannot see.
UPDATE
A comment indicated that my pkg name was wrong, but it didn't matter regardless, as the below updated screenshot shows
Regards,
If you want to write the main function without any parameters, it has to be outside of the class like this:
package org.example
fun main() {
println("Hello World!")
}
Note that in this case, the main function will be inside an artificial implicit class that is called like the containing file suffixed with Kt. This means, that in your example where you place the main function inside a file AppKt.kt, the resulting main class is called AppKtKt.
If you want to have the main function inside a class, you have to specify a parameter for the arguments. Also, it needs to be static, i.e. it has to be inside an object and must be annotated with #JvmStatic.
This can be done by declaring an object:
object AppKt {
#JvmStatic
fun main(args: Array<String>) {
println("Hello World!")
}
}
Or you can put the main function inside the companion object of a class:
class AppKt {
companion object {
#JvmStatic
fun main(args: Array<String>) {
println("Hello World!")
}
}
}
In Kotlin, your main function goes outside any class definition.

Kotlin script - "extension method" is a member and an extension at the same time

I have Kotlin some code that works as a class but when I try and run it as a Kotlin script I am getting " error: 'getNameAndVersion' is a member and an extension at the same time. References to such elements are not allowed"
enum class Env { Test, Sit }
data class ImageVersions(val apiName: String, val versions: Map<Env, String?>)
fun String.getNameAndVersion() = substringBefore(':') to substringAfter(':')
val testList = listOf("api-car-v1:0.0.118", "api-dog-v1:0.0.11", "api-plane-v1:0.0.36")
val sitList = listOf("api-car-v1:0.0.119", "api-plane-v1:0.0.37", "api-dog-v1:0.0.12")
getVersions(
mapOf(
Env.Test to testList,
Env.Sit to sitList
)
).forEach(::println)
fun getVersions(envMap: Map<Env, List<String>>): List<ImageVersions> {
val envApiNameMap = envMap.mapValues { it.value.associate(String::getNameAndVersion) }
val allApiNames = envApiNameMap.flatMap { it.value.keys }.distinct()
return allApiNames.map { apiName ->
ImageVersions(apiName, envApiNameMap.mapValues { it.value[apiName] })
}
}
I don't think I'm doing anything wrong with the way I'm using the method reference but according to my compiler I'm wrong. Appreciate some help with this. thanks
kotlinc -script .\myscript.kts
error: 'getNameAndVersion' is a member and an extension at the same time. References to such elements are not allowed
I don't have any experience with scripts but this error occurs when you try to reference a function inside a class that is also an extension function. (Here it is pointing to String::getNameAndVersion). Maybe when you run a script, the entire code is wrapped inside a class and then executed.
To fix this you can do one of the following:
Convert the function to a normal function which accepts a String parameter.
fun getNameAndVersion(s: String) = s.substringBefore(':') to s.substringAfter(':')
And replace String::getNameAndVersion with just ::getNameAndVersion in associate function
Other option is to directly call the function in the associate's lambda instead of passing a reference of this function.
.associate { it.getNameAndVersion() }

Generic inline function

Let's say I have an object which helps me to deserialize other objects from storage:
val books: MutableList<Book> = deserializer.getBookList()
val persons: MutableList<Person> = deserializer.getPersonList()
The methods getBookList and getPersonList are extension functions I have written. Their logic is allmost the same so I thought I may can combine them into one method. My problem is the generic return type. The methods look like this:
fun DataInput.getBookList(): MutableList<Book> {
val list = mutableListOf<Book>()
val size = this.readInt()
for(i in 0 .. size) {
val item = Book()
item.readExternal(this)
list.add(item)
}
return list
}
Is there some Kotlin magic (maybe with inline functions) which I can use to detect the List type and generify this methods? I think the problem would be val item = T() which will not work for generic types, right? Or is this possible with inline functions?
You cannot call the constructor of a generic type, because the compiler can't guarantee that it has a constructor (the type could be from an interface). What you can do to get around this though, is to pass a "creator"-function as a parameter to your function. Like this:
fun <T> DataInput.getList(createT: () -> T): MutableList<T> {
val list = mutableListOf<T>()
val size = this.readInt()
for(i in 0 .. size) {
val item = createT()
/* Unless readExternal is an extension on Any, this function
* either needs to be passed as a parameter as well,
* or you need add an upper bound to your type parameter
* with <T : SomeInterfaceWithReadExternal>
*/
item.readExternal(this)
list.add(item)
}
return list
}
Now you can call the function like this:
val books: MutableList<Book> = deserializer.getList(::Book)
val persons: MutableList<Person> = deserializer.getList(::Person)
Note:
As marstran mentioned in a comment, this requires the class to have a zero-arg constructor to work, or it will throw an exception at runtime. The compiler will not warn you if the constructor doesn't exist, so if you pick this way, make sure you actually pass a class with a zero-arg constructor.
You can't initialize generic types, in Kotlin or Java. At least not in the "traditional" way. You can't do this:
val item = T()
In Java, you'd pass a Class<T> and get the constructor. Very basic example of that:
public <T> void x(Class<T> cls){
cls.getConstructor().newInstance(); // Obviously you'd do something with the return value, but this is just a dummy example
}
You could do the same in Kotlin, but Kotlin has a reified keyword that makes it slightly easier. This requires an inline function, which means you'd change your function to:
inline fun <reified T> DataInput.getBookList(): MutableList<T> { // Notice the `<reified T>`
val list = mutableListOf<T>() // Use T here
val size = this.readInt()
for(i in 0 .. size) {
// This is where the initialization happens; you get the constructor, and create a new instance.
// Also works with arguments, if you have any, but you used an empty one so I assume yours is empty
val item = T::class.java.getConstructor().newInstance()!!
item.readExternal(this) // However, this is tricky. See my notes below this code block
list.add(item)
}
return list
}
However, readExternal isn't present in Any, which will present problems. The only exception is if you have an extension function for either Any or a generic type with that name and input.
If it's specific to some classes, then you can't do it like this, unless you have a shared parent. For an instance:
class Book(){
fun readExternal(input: DataInput) { /*Foo bar */}
}
class Person(){
fun readExternal(input: DataInput) { /*Foo bar */}
}
Would not work. There's no shared parent except Any, and Any doesn't have readExternal. The method is manually defined in each of them.
You could create a shared parent, as an interface or abstract class (assuming there isn't one already), and use <reified T : TheSharedParent>, and you would have access to it.
You could of course use reflection, but it's slightly harder, and adds some exceptions you need to handle. I don't recommend doing this; I'd personally use a superclass.
inline fun <reified T> DataInput.getBookList(): MutableList<T> {
val list = mutableListOf<T>()
val size = this.readInt()
val method = try {
T::class.java.getMethod("readExternal", DataInput::class.java)
}catch(e: NoSuchMethodException){
throw RuntimeException()
}catch(e: SecurityException){
throw RuntimeException()// This could be done better; but error handling is up to you, so I'm just making a basic example
// The catch clauses are pretty self-explanatory; if something happens when trying to get the method itself,
// These two catch them
}
for(i in 0 .. size) {
val item: T = T::class.java.getConstructor().newInstance()!!
method.invoke(item, this)
list.add(item)
}
return list
}

How to define an extension function for a companion object of a typealias?

I thought typealiases were the same as the original type, just a different name.
I figure typealiases have the same references as the original type.
typealias Celsius = Double
fun Double.Companion.foo() {} // Works
fun Celsius.Companion.foo() {} // Does not work
Here, Companion is accessible from Double but Celsius gives an unresolved reference error.
No, you can't access to the companion objects via typealias. One possible workaround to create one more typealias for concrete companion:
typealias CelsiusCompanion = Double.Companion
After that you can use it as following:
fun CelsiusCompanion.foo() {}
If you want to define an extension function, it is not possible as hluhovskyi already stated, but things are differently if you just want to invoke functions of a companion object.
There are two ways of accessing functions and properties within a companion object. You can either specify the access explicitely or implicitely. The implicit way works with a typealias the explicit one does not.
Consider this minimal example:
class ClassWithCompanion{
companion object {
fun sayHello() {
println("Hello")
}
}
}
typealias Alias = ClassWithCompanion
fun main(args: Array<String>) {
ClassWithCompanion.sayHello() // implicit
ClassWithCompanion.Companion.sayHello() // explicit
Alias.sayHello() // implicit (works)
Alias.Companion.test() // explicit (does not work)
}

How to pass vararg as array to function in Kotlin?

I want to pass vararg from the buy function to the drive function
but I get
a compile error:
required Array<T> found Array<out T>
code:
class Car
fun buy(vararg cars: Car) {
drive(cars) //compile error
}
fun drive(cars: Array<Car>) {
//...
}
The precise error is:
Type mismatch.
Required: Array<Car>
Found: Array<out Car>
The problem is that when you have a vararg cars: Car function parameter, its type inside the function is Array<out Car>, which basically represents a read-only array of the arguments the function was called with - after all, it wouldn't really be a nice thing to start modifying the arguments you got as a vararg (function parameters in Kotlin are read-only vals, and this is something very similar).
But your drive function requires a regular Array<Car>, which of course is writable as well. So if you wish to keep these function signatures, you'll need to create a new array that contains everything from your read-only array. This can be created using the following syntax:
drive(arrayOf(*cars))
This calls the arrayOf function we usually create arrays with, which takes a vararg parameter, and uses the spread operator which is a way to pass in the elements of an array (which cars is, inside the buy function) as the vararg parameters (docs here).
While all this may sound complicated, it's actually a very efficient solution, as it just uses Array.copyOf under the hood in the bytecode.
Another solution would be to change drive to fun drive(Array<out Car>) { ... }. This of course means that the cars inside drive cannot be modified but avoids the copying.
fun foo(vararg strings: String): Array<out String> {
return strings
}
// Define a var
var yourVar: Array<out String> = foo("a", "b", "c")
// Use var in Java method `public void javaMethod(String... someParam)() {}`
javaMethod(*yourVar)
override fun checkMatch(vararg cards: SetsCard): Int {
return if (isSet(*cards)) 16 else -2
}
private fun isSet(vararg cards: SetsCard) : Boolean {
if (cards.size == 3) {
return true
}
return false
}
Essencially you need the vararg keywork before the variable name.
fun args(vararg cars: Car) {
//use cars as an array
}