Stuck on the basics: Convert String to Long

Okay, how do you do this?

I’m playing around with the Ninja Web Framework (converting the example to Kotlin) and I’ve hit upon this annoyingly basic problem: converting a string to a Long.

  fun asyncEcho(ctx: Context) : Result? {   executorService.schedule(object: Runnable {

           override fun run() {
           ctx.returnResultAsync(Results.json()?.render(ctx.getParameter(“message”)))
           }

  &nbsp;&nbsp;}, <strong>ctx.getParameter("timeout")</strong>, TimeUnit.MILLISECONDS) <strong>// &lt;-- The ctx.getParameter call returns a string; I need it as as a long</strong>

  &nbsp;&nbsp;return Results.async()

  }


How do you go about doing this?

Thanks.

:8}

You can use ".toLong()" on any string

If the string is in fact “String?”, use “!!” or a null check

also you could call executorService.schedule function with a SAM construct

``

fun asyncEcho(ctx: Context) : Result? {
  executorService.schedule({
           ctx.returnResultAsync(Results.json()?.render(ctx.getParameter(“message”)))
           }, ctx.getParameter(“timeout”)!!.toLong() /*As Andrey suggest */, TimeUnit.MILLISECONDS) // <– The ctx.getParameter call returns a string; I need it as as a long
  return Results.async()
  }

Mmmm.

I tried this:

  fun asyncEcho(ctx: Context) : Result? {   executorService.schedule(object: Runnable {            override fun run() {            ctx.returnResultAsync(Results.json()?.render(ctx.getParameter("message")))            }   }, ctx.getParameter("timeout")?.toLong(), TimeUnit.MILLISECONDS) //<-- Using toLong()   return Results.async()   }

And  I got this:

Kotlin: None of the following functions can be called with the arguments supplied: public abstract fun schedule(p0: java.lang.Runnable, p1: jet.Long, p2: java.util.concurrent.TimeUnit): java.util.concurrent.ScheduledFuture<out jet.Any?> defined in java.util.concurrent.ScheduledExecutorService public abstract fun <V> schedule(p0: java.util.concurrent.Callable<V>, p1: jet.Long, p2: java.util.concurrent.TimeUnit): java.util.concurrent.ScheduledFuture<V> defined in java.util.concurrent.ScheduledExecutorService

There's something about going between Java and Kotlin I'm not quite getting ... :|

As mentioned above, use "!!", not "?.": the former gives you "Long", the latter — "Long?"

Yup! That did it.

Thanks!

:slight_smile: