the content in the parameter of the main fun in kotlin - kotlin

I'm a beginner of Kotlin language and I want to understand everything in this programming language so.
1- what mean of the args: Array<String> in the parameter of the main function ?
2- why compilation error is shown when this line is removed ?

The main(Array<String>) function is an entry point of a program. The strings passed are the command-line arguments.
That is, when you run something like
$ java myprogram foo bar
the main function is called with ["foo", "bar"] as the argument.
This is very much the same thing that Java does, and Java emulates C in this regard.

I keep replacing it with vararg everywhere
object Application {
#JvmStatic
fun main(vararg args: String) {
Micronaut.build()
.packages("example")
.mainClass(Application::class.java)
.start()
}
}

fun main(args : Array<String>) {
println("Hello, world!")
}
In this example, a function named main is defined which returns Unit and takes an Array of strings as a parameter.

Depend on Java convention, this Kotlin code bellow's the same with Java code.
Kotlin code:
fun main(args : Array<String>) {
println("Hello, world!")
}
Java code:
public static void main(String[] args) {
println("Hello, world!");
}
According to #9000 "The main(Array) function is an entry point of a program. The strings passed are the command-line arguments." Each program must have the start entry point. That's just a convention to get the function call parameter.

Related

How to get declaring KtFile/KtClass using KtCallExpression?

I'm using kotlin compiler to analyze kotlin source file and I need to get where the method is defined using KtCallExpression.
KtCallExpression in question is retrieved from method call.
For better explanation, here's example, if there's source file named Main.kt with contents:
fun main() {
f1()
Test.f2()
}
fun f1() {
}
class Test {
companion object {
fun f2() {
}
}
}
when analyzing function main, it will produce 2 KtCallExpression, one for f1() and other for f2()
However I'm not sure how to get file Main.kt from KtCallExpression for f1(), or class Test from KtCallExpression for f2()
I tried to look for its documentations, however Kotlin compiler doesn't contain enough docs. I've also tried bunch of auto completion suggested by Intellij Idea but none of those doesn't seem to lead me to original declaration.

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.

what's the difference between kotlin main functions

what's the difference between this two types of main function in kotlin
fun main(args: Array<String>) {
print("Hello World!")
}
and
fun main() {
print("Hello World!")
}
The syntax with the args is used to pass the parameters to the module from command line interface or from outside program. If you don't need it, you can omit it.
If you are getting started with kotlin I strongly recommend you to look at kotlin official website.
With the first option you could pass arguments when running your program

Why Reciver function of String get intialized in kotlin

I was playing around with Kotlin Recivers until i get stuck with this block of code
assume this varaible
var greet: String.() -> Unit = {
println("Hello $this")
}
String is the receiver
() -> Unit is the function type
And we can call it in that form , which is clear enough for me
"my extintion text".greet() //--> prints "Hello extintion text"
But
with this approch i can't get to understand the trick behind intializing the greet
variable as we havn't set any parameter and how Kotlin idintiy my
passed value ("my extintion text") as A string
and use greet exintion on it
greet("also my extintion text") //--- prints "Hello also extintion text"
Your lambda when decompiled, shows this:
public interface Function1<in P1, out R> : Function<R> {
/** Invokes the function with the specified argument. */
public operator fun invoke(p1: P1): R
}
And this is how the Main method looks like:
public final class MainKt {
public static final void main() {
Function1 greet = (Function1)null.INSTANCE; //Your 'greet' is of Function1 type which has 'invoke' method.
greet.invoke("my extintion text"); //Calling the 'invoke' method of Function1.
}
The compiler calls the invoke() method passing your "my extintion text" string and prints it
There is no trick, the compiler will eventually use the invoke() method pass in the parameters just like any regular method.
Based on #Tasser answer i can understand how it works now
but i would illustrate in diffrent manner so it might be more persuasive for some one
starting with the declaring A simple lamba experation
var greet: String.() -> Unit
when we defined our variable in such manner our variable as #Tasser briefed is now A Functional type that implements "Function1 interface" when decompiled
it has only one operator function public operator fun invoke(p1: P1): R
and here we came pivotal part operator fun i(p1: P1): R
this function invoked immediately caues it mathed with argument type
and because our experation {
println("Hello $this")
} is decombiled through implementing the "Function1 interface" with operator fun invoke(p1: P1): R , thats is why my implementaion get executed

how to call a (samename) method declared outside with(){} scope in 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.