Question

I'm really new to Scala and I'm not even able to concatenate Strings. Here is my code:

object RandomData {

private[this] val bag = new scala.util.Random

def apply(sensorId: String, stamp: Long, size: Int): String = {
  var cpt: Int = 0
  var data: String = "test"
  repeat(10) {
    data += "_test"
  }
  return data
}
}

I got the error:

type mismatch;
 found   : Unit
 required: com.excilys.ebi.gatling.core.structure.ChainBuilder

What am I doing wrong ??

Was it helpful?

Solution

repeat is offered by Gatling in order to repeat Gatling tasks, e.g., query a website. If you have a look at the documentation (I wasn't able to find a link to the API doc of repeat), you'll see that repeat expects a chain, which is why your error message says "required: com.excilys.ebi.gatling.core.structure.ChainBuilder". However, all you do is to append to a string - which will not return a value of type ChainBuilder.

Moreover, appending to a string is nothing that should be done via Gatling. It looks to me as if you are confusing Gatling's repeat with a Scala for loop. If you only want to append "_test" to data 10 times, use one of Scala's loops (for, while) or a functional approach with e.g. foldLeft. Here are two examples:

/* Imperative style loop */
for(i <- 1 to 10) {
  data += "_test"
}

/* Functional style with lazy streams */
data += Stream.continually("_test").take(10).mkString("")

OTHER TIPS

Your problem is that the block

{
  data += "_test"
}

evaluates to Unit, whereas the repeat method seems to want it to evaluate to a ChainBuilder.

Check out the documentation for the repeat method. I was unable to find it, but it's probably reasonable to assume that it looks something like

def repeat(numTimes: Int)(thunk: => ChainBuilder): Unit

I'm not sure if the repeat method does anything special, but with your usage, you could just use this block instead of the repeat(10){...}

for(i <- 1 to 10) data += "_test"

Also, as a side note, you don't need the return keyword with scala. You can just say data instead of return data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top