I don’t know JS so I don’t really know what the JS version does. I think you are trying to get the command line arguments or if there is none use 9000. The args
parameter of main
contains those. So you could use code like this:
val port = args.getOrNull(2)?.toIntOrNull() ?: 9000
Those 2 errors have to do with the types of the parameters. Normally Kotlin can deduce most of the types from some context, e.g if you pass an anonymous function as an argument, the compiler can find the types of the parameters of the anonymous function. The type of the server.listen method would specify the type of the parameters. But because you are using dynamic types, the compiler does not know about those so you must specify them.
As far as I can tell you want to use the result of map.get(ext)
and if the result is null use "text/plain"
. In Kotlin you can not use the ||
operator for that as it is normally used for booleans. The Kotlin way of writing this is map[ext] ?: "text/plain"
. Note you can use brackets to access maps, lists, etc. You should take a look at the elvis operator.
Two more things. By this question and the other one you asked I can guess that you are quite new to programming or at least programming using a different language than JavaScript. Most of the questions you had could have been answered by the Kotlin documentation, so I’d suggest you take a look at it. It is quite well written. You should definitely look at the Getting Started and Basics part: https://kotlinlang.org/docs/reference/basic-syntax.html
The other is the way you ask your questions. There is nothing wrong with being new to something or learning something new, but most people me normally included would probably skip both of your questions right away. Let me try to explain why. Reading and understanding code takes time. A lot of time. Most of the time programing is spent reading code (80% or so). Your question has 120 lines of code. There might be some questions which are so complex, that this is really necessary, but most often this is not the case. Try to spent some time to isolate the problem and just paste the required code. It might also be a good idea to create a new project in which you recreate you problem. In your case you could create 2 functions
fun call(func: (exists: Boolean) -> Unit){
func(true)
func(false)
}
fun test(){
call(fun (exist) { ... })
}
At that point you would have seen, that this would work in the small test project, for reasons I explained above. Than you can try to find more anwsers or ask you question here. “I have the above code using Kotlin2JS and for some reason I get this error”
Also google first and ask later. I am 100% sure there have been people before you who had the same problem you have. Obviously not in 100% the same context, but similar enough that answers to their question would have helped you too. So look at that. This goes along the same way of trying to ask shorter more concise questions. If you can ask an more abstract question there is a higher chance someone else asked it before you. If you don’t find an answer you can still come here and ask.
I hope you can take the last part as friendly criticism. No one here minds helping other people, but I am pretty sure there are more people here willing to help if you put a bit more effort into asking the question as it makes it easier to help you