I'm trying to load a bunch of com.mongodb.DBObject objects in to a Vaadin BeanItemContainer to display in a table. I'm getting stuck on the translation of the constructor from Java to Scala.
The constructor definition is:
BeanItemContainer(Class<? extends BT> type)
This passes the scala compiler:
val bic = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
However, when I try to add an item:
mtl.toArray.foreach {t => bic.addBean(t)}
I get the following error:
[ERROR]com/sentientswarm/traderdashboard/UploadTradesWindow.scala:140: error: type mismatch;
found : t.type (with underlying type com.mongodb.DBObject)
required: ?0 where type ?0
mtl.toArray.foreach {t => bic.addBean(t)}
Any thoughts/suggestions?
UPDATE:
Tried:
val bic: BeanItemContainer[DBObject] = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
Result:
[ERROR]com/sentientswarm/traderdashboard/UploadTradesWindow.scala:140: error: type mismatch;
found : java.lang.Class[?0(in value bic)] where type ?0(in value bic)
required: java.lang.Class[_ <: com.mongodb.DBObject]
val bic: BeanItemContainer[DBObject] = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
^
Thanks,
John
Any reason you're using Class.forName? I don't think the compiler can infer the type from the returned object from that call, it would just be Class[_]. If you use classOf, it should let the compiler determine the type:
val bic = new BeanItemContainer[DBObject](classOf[DBObject]))
In other words: DBObject.class in Java translates to classOf[DBObject] in Scala.
Try this:
val bic: BeanItemContainer[BT] = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
By the way, you removed the "^" marker of where in the line the error is. Please, keep it when pasting error messages.
Related
I'm new to Scala. I have a Scala function, and one of its arguments uses "Option":
def generateTimeSnippet(startOfSentence: Double, endOfSentence: Option[Double]): (Double, Option[Double]) = {
...
}
When I call this function, I give literal values to the arguments:
val snippets = generateTimeSnippet(startOfSentence = 10d, endOfSentence = 20.5)
But this results in a type mismatch error:
"type mismatch; found : Double(10.0) required: Option[Double]"
I find this very odd. Why is this happening? Am I not supposed to be allowed to use Doubles for an argument that is defined as Option[Double]?
Of course, for an argument of the type Option[Double], you can not send in the type Double. They are different types.
You can send in Some(20.5) which is of the type Option[Double]
I get scalac exception on attempt to use upper bound and context bound at the same time. Is it even allowed ? I'm on Scala 2.11.8
Consider this
import spray.json._
abstract class CrossRefMessage
case class CrossRefResponse[T <: CrossRefMessage](status: String, `message-type`: String, `message-version`: String, message: T)
implicit def CrossRefResponseFormat[T <: CrossRefMessage](implicit reader: JsonFormat[T]) = jsonFormat4(CrossRefResponse.apply[T])
on compilation I get
Error:scalac: Error: type mismatch;
found : String
required: co.zzzz.server.JournalsManager.CrossRefMessage
scala.reflect.internal.Types$TypeError: type mismatch;
found : String
required: co.zzzz.server.JournalsManager.CrossRefMessage
at scala.tools.nsc.typechecker.Contexts$ThrowingReporter.handleError(Contexts.scala:1402)
at scala.tools.nsc.typechecker.Contexts$ContextReporter.issue(Contexts.scala:1254)
at scala.tools.nsc.typechecker.Contexts$Context.issue(Contexts.scala:573)
at scala.tools.nsc.typechecker.ContextErrors$ErrorUtils$.issueTypeError(ContextErrors.scala:106)
What am I missing ?
UPDATE: It looks like magic to me but error goes away if I change position of field message: T. It compiles if it's on 1st or 2nd position. Can anyone explain why ?
I asked a longer question, but it seems it's too much code for people to sort through so I've created this question to focus on one smaller, specific problem I'm facing regarding use of macros in Scala.
Consider the following code snippet:
val tpe = weakTypeOf[T]
val companion = tpe.typeSymbol.companionSymbol
val fields = tpe.declarations.collectFirst {
case m: MethodSymbol if m.isPrimaryConstructor => m
}.get.paramss.head
val toMapParams = fields.map { field =>
val name = field.name
val decoded = name.decoded
q"$decoded -> t.$name"
}
Note that fields is just the list of parameters for the primary constructor of a case class in this code. Where I'm confused is the result of the quasiquote q"$decoded -> t.$name". What does this mean exactly? And what type should it have? I'm getting a compile error stating the following:
Multiple markers at this line
- Implicit conversions found: q"$decoded -> t.$name" => Quasiquote(q"$decoded -> t.
$name")
- type mismatch; found : field.NameType required: c.universe.TermName
- type mismatch; found : field.NameType required: c.universe.TermName
Can anyone explain this error? Thanks.
The type of fields is List[Symbol], which means that the type of names of those fields is inconclusive (unknown whether it's a TermName or TypeName). This means that you can't insert such names essentially anywhere in a quasiquote.
A simple fix would be to do val name = field.name.toTermName, explicitly telling the compiler that it's looking at a term name, so that quasiquote knows how to process it.
Hi all I am fairly new to Scala coming from C#.
I am attempting to write my own version of accumulate ( fold) I am wondering why I am running into some issues with the following:
def accumulate[T](list : List[T], initial: T, f: (T, T) => T) : T = {
#tailrec def loop[T](list: List[T], accum: T) : T =
if(list.length == 0)
accum
else{
val head : T = list.head
val res : T = f(accum,head)
loop[T](list.tail, res)
}
loop(list,initial)
}
I am getting the following error:
type mismatch;
found : accum.type (with underlying type T)
required: T
val res : T = f(accum,head)
^
I cant see how I have a type mismatch considering everything is type T.
Any thoughts / help would be appreciated.
Blair
You should just remove type parameter from loop method. Replace loop[T] with loop.
With loop[T] you are creating new type parameter with name T, so T outside loop method and T in loop method are different type aliases with the same name.
It's called shadowing.
See these answers for similar problems:
Scala type parameter error, not a member of type parameter
Scala, Extend object with a generic trait
Generic type inference in Scala
The problem is that with the inner function loop you are defining a new type T that is shadowing the outer type T.
The compiler sees them as defining different types. If you simply remove the T type parameter from loop (including the recursive call loop(list.tail, res)) you should find it compiles just fine.
I'm trying to use the sequenceU function on a List of Validation objects, but I keep on getting the error:
type mismatch;
found : G.M[List[G.A]]
required: scalaz.package.ValidationNEL[com.gaiam.gcsis.ws.validation.DataError,List[com.gaiam.gcsi.entities.plan.Service]]
[ERROR] val services: ValidationNEL[DataError, List[Service]] = valServices.sequenceU
I have two types here. The DataError class is a scala class (non-case class). The Service class is a Java class.
And here is the code where I receive this error.
val valServices: List[ValidationNEL[DataError, Service]] = XXX
val services: ValidationNEL[DataError, List[Service]] = valServices.sequenceU
Any thoughts on why I keep getting the type mismatch? I am on 7.0.0-M7.
You are probably on a 2.9.x version of Scala. Add -Ydependent-method-types as a compiler option