I'm using a parameter for my ramp value as per the docs,
val rampUpRate = Integer.getInteger("ramp", 1)
setUp(
scn.users(10).ramp(rampUpRate).protocolConfig(httpConf)
)
But when I run gatling, I'm getting an error:
09:57:35.695 [ERROR] c.e.e.g.a.ZincCompiler$ - /Gatling/user-files/simulations/clients/com/mydomain/www/stress/RecordedSimulation.scala:1088: overloaded method value ramp with alternatives:
(duration: akka.util.Duration)com.excilys.ebi.gatling.core.scenario.configuration.ConfiguredScenarioBuilder <and>
(duration: Long)com.excilys.ebi.gatling.core.scenario.configuration.Configured
ScenarioBuilder
cannot be applied to (java.lang.Integer)
I thought I could simply cast to Long before using the parameter
val rampUpRate = Integer.getInteger("ramp", 1)
setUp(
scn.users(10).ramp((Long) rampUpRate).protocolConfig(httpConf)
)
but this still errors:
09:57:35.695 [ERROR] c.e.e.g.a.ZincCompiler$ - /Gatling/user-files/simulations/clients/com/mydomain/www/stress/RecordedSimulation.scala:1088: \sanctuarySpa\com\sanctuaryspa\www\stress\RecordedSimulation.scala:1088:
value rampUpRate is not a member of object Long
10:05:34.915 [ERROR] c.e.e.g.a.ZincCompiler$ - scn1.users(10).ramp((Long) rampUpRate).protocolConfig(httpConf),
Any suggestions why following the documentation, or the explicit cast to long don't work?
Try using rampUpRate.toLong to cast to a Long (or the more general cast rampUpRate.asInstanceOf[Long])
(Long) rampUpRate is seen by the compiler as trying to perform Long.rampUrRate() e.g. applying function rampUpRate to object Long, hence the error message
That's my fault: the wiki page is not up to date.
What happens is that you have a java.lang.Integer while the method takes a scala Long. java.lang.Long can be implicitly converted into scala Long, but not java.lang.Integer.
The proper way would be val rampUpRate = java.lang.Long.getLong("ramp", 1L)
PS: I've fixed the doc.
Related
The following statement in Scala throws a Runtime error:
val a = 10: Object
error: the result type of an implicit conversion must be more specific than Object
val a = 10: Object
^
If the above-mentioned implicit conversion is not allowed in Scala, then why doesn't Scala throws the error at compile time?
Why do you think this is a runtime error? It is not:
$ scalac impl.scala
impl.scala:3: error: the result type of an implicit conversion must be more specific than Object
val a = 10: Object
^
one error found
I got following errors after upgraded play framework from 2.5 to 2.6.
(It was able to compile with 2.5)
Scala version: 2.11.8
ambiguous reference to overloaded definition,
[error] both method emailAddress in trait Constraints of type => play.api.data.validation.Constraint[String]
[error] and method emailAddress in trait Constraints of type (errorMessage: String)play.api.data.validation.Constraint[String]
[error] match argument types (String)
[error] Constraints.emailAddress("test#sample.com")
^
Is there any way to use methods under Constraints with Play 2.6?
import play.api.data.validation._
val result = Constraints.emailAddress("test#sample.com")
emailAddress is a method which internally calls the another overridden method emailAddress(errorMessage: String = "error.email") method so if you call emailAddress with parameter that will give compilation error, so you can test it like this
val result = Constraints.emailAddress
result("test#sample.com")
I get a weird compilation error in the small Scala exercise I am working on.
I have this method that is supposed to keep on asking user input until a correct answer is provided. Alas I am stumbled at the first case in my pattern matching:
override def guess(guess: Int):Unit = {
val guessIndex = binary(array, guess)
guessIndex match {
case -1 => {
val nextAttempt = StdIn.readLine(s"Please be attentive $guess is outside the search range"
+" (0 to $upperBound). Try again: \n");
val a = validateType[Int](nextAttempt)
guess(a)
}
}
}
The IDE underlines guess(a) with the error "Int doesn't take parameters". Running sbt compile from the console confirms this error:
> compile
[info] Compiling 2 Scala sources to /home/vgorcinschi/Documents/eclipseProjects/Algorithms/Chapter 1 Fundamentals/algorithms1_4_34/target/scala-2.12/classes...
[error] /home/vgorcinschi/Documents/eclipseProjects/Algorithms/Chapter 1 Fundamentals/algorithms1_4_34/src/main/scala/ca/vgorcinschi/algorithms1_4_34/hotandcold/HotAndColdImpl.scala:23: Int does not take parameters
[error] guess(a)
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 0 s, completed 6-May-2017 6:47:58 PM
There are few different Stackoverflow tickets for the same error message, but they're for different scenarios. In mine here it looks like a method who takes an Int parameter is being rejected. If you could please give me a hint this would help me a lot.
Rename the guess parameter (or the method name, so it's something different) - the parameter is the first guess in scope, so the compiler thinks you're trying to call it as a function.
I am not sure how to get past this "No matching Shape found" error, apart from writing lots of boilerplate.
The basic idea illustrated in the Gist is that I have a very basic version of a method (works, but is very specific), then a version that takes the mapper parameter and is more generic (works too, but is specific to one particular type), and then a third version which takes a type parameter and would be very useful, but doesn't compile because of this error.
Basic method:
def updatePD_FirstNames(id: ids.PersonalDetailsId, firstNames: StringLtd30): Future[Int] = {
Better method:
def updatePD_SL(id: ids.PersonalDetailsId, mapper: tables.PersonalDetails => tables.profile.api.Rep[StringLtd30], sl: StringLtd30): Future[Int] = {
Ideal method (but doesn't compile):
def updatePD_X[X](id: ids.PersonalDetailsId, mapper: tables.PersonalDetails => tables.profile.api.Rep[X], sl: X): Future[Int] = {
```
[server] $ compile
[info] Compiling 1 Scala source to ... target\scala-2.12\classes...
[error] ...schema\DbProxy.scala:688: No matching Shape found.
[error] Slick does not know how to map the given types.
[error] Possible causes: T in Table[T] does not match your * projection,
[error] you use an unsupported type in a Query (e.g. scala List),
[error] or you forgot to import a driver api into scope.
[error] Required level: slick.lifted.FlatShapeLevel
[error] Source type: slick.lifted.Rep[X]
[error] Unpacked type: T
[error] Packed type: G
[error] val q2: Query[tables.profile.api.Rep[X], X, Seq] = q1.map(mapper)
[error] ^
[error] one error found
[error] (server/compile:compileIncremental) Compilation failed
[error] Total time: 4 s, completed 23-Mar-2017 11:15:47
```
Full code at https://gist.github.com/aholland/0845bf29d836d672d006ab58f5f1c73c
The only obvious problem I can see in the code you've posted is that X is unconstrained. It could be any type, includes ones that Slick doesn't know how to process.
What you can do is add a context bound on X. The bound you probably want is BaseTypedType, which is a "typed type" Slick uses to identify types it can work with. It's described from 11:30 in https://www.youtube.com/watch?v=tS6N5AaZTLA
You'd use it like this:
import slick.ast.BaseTypedType
def updatePD[X : BaseTypedType](
id: Long,
selector: PersonTable => Rep[X],
newValue: X
): DBIO[Int] =
people.filter(_.id === id).map(selector).update(newValue)
What that means is that when you use the method...
updatePD(anId, _.name, "Alice")
...the compiler has to prove to itself that whatever X you use, there is an approproate type representation in Slick.
This is also from Richard, but the exchange took place on gitter.
The only trouble with the first answer is that by demanding an implicit of type BaseTypedType[X] the context bound forces client code for optional columns to provide an implicit of type BaseTypedType[Option[X]] even when BaseTypedType[X] is already available.
This is unnecessary. Slick handles optional columns for you and if you provide an implicit for BaseTypedType[X] you are providing enough for it to handle columns of type Option[X].
So the context bound, while it works, is more demanding than necessary and results in having to write implicits in the client-code that involve directly referencing null and replicating logic already built into Slick. Not good.
The answer is to declare the implicit parameter as a named implicit parameter (called shape below) in its own parameter list, i.e. in long-form, not using the context bound short-hand :BaseTypedType. Then you can specify the more complicated but less demanding constraint used below.
So the solution is:
def updatePD[X] (id: Long, selector: PersonTable => Rep[X], newValue: X)
(implicit shape: Shape[_ <: FlatShapeLevel, Rep[X], X, _]): DBIO[Int] = {
people.filter(_.id === id).map(selector).update(newValue)
}
Understanding why shape has the exact type Shape[_ <: FlatShapeLevel, Rep[X], X, _] depends on an intimate understanding of Slick's types and implicit mechanisms. Richard may yet write a blog post on that!
Straight forward problem that I don't see what I'm doing wrong in - some type mismatch somewhere. Basically trying to set a default datatype of Long on parameters that are coming in from a web request. Here's the code:
val startTs:Long = params.getOrElse("start_ts", DateTime.yesterdayAsEpoch).toLong
val endTs:Long = params.getOrElse("end_ts", DateTime.todayAsEpoch).toLong
My DateTime helper code:
def todayAsEpoch: Long = {
val c = Calendar.getInstance(TimeZone.getTimeZone("EST"))
c.setTime(new java.util.Date())
c.set(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),0,0,0)
c.getTimeInMillis / 1000L
}
def yesterdayAsEpoch: Long = {
val c = Calendar.getInstance(TimeZone.getTimeZone("EST"))
c.setTime(new java.util.Date())
c.set(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),0,0,0)
((c.getTimeInMillis / 1000L) - 86400)
}
And finally, the error:
value toLong is not a member of Any
[error] val startTs:Long = params.getOrElse("start_ts", DateTime.yesterdayAsEpoch).toLong
[error] ^
[error] /vagrant/src/main/scala/com/myapp/api/controllers/FooController.scala:437: value toLong is not a member of Any
[error] val endTs:Long = params.getOrElse("end_ts", DateTime.todayAsEpoch).toLong
[error] ^
[error] two errors found
[error] (compile:compile) Compilation failed
You did not say what params is. It looks like it might be a Map[String, X] with some type X. params.getOrElse(key, someLong) will considered to have the best common supertype of X and Long which happens to be Any, according to the error message, and which has no toLong method. As your default value happens to be Long already, and so don't need to be converted, I guess there is a toLong method on X.
If it is so, then you should convert the value retrieved from params to Long (when there is such a value), before providing the default value. That would be :
params.get("key").map(_.toLong).getOrElse(defaultValue)
I'm guessing params is a Map[String, Something], and that Something isn't always a numeric type. (String?) In any case, when you call params.getOrElse, it's inferring a common type between Something and Long, and finding Any, which is why you can't call toLong on it.