score:5
You've bumped into one of the cases where Scala's attempt to unify primitives and Objects breaks down. Since Int
in Scala represents the Java primitive type int
, it can't have any traits mixed into it. When doing asInstanceOf, the Scala compiler autoboxes the Int
into a java.lang.Integer
:
scala> val a: Int with Test = 10.asInstanceOf[Int with Test]
a: Int with Test = 10
scala> a.getClass
res1: Class[_ <: Int] = class java.lang.Integer
However, autoboxing doesn't happen when declaring types, so you have to do it by hand:
scala> case class Foo(x: Integer with Test)
defined class Foo
But then the compiler type checker won't autobox before checking the types:
scala> Foo(a)
<console>:12: error: type mismatch;
found : Int with Test
required: Integer with Test
Foo(a)
^
So you would have to declare your variable as Integer with Test
:
scala> val a: Integer with Test = 10.asInstanceOf[Integer with Test]
a: Integer with Test = 10
scala> Foo(a)
res3: Foo = Foo(10)
or use a cast when calling the case class:
val a : Int with Test = 10.asInstanceOf[Int with Test]
scala> a: Int with Test = 10
scala> Foo(a.asInstanceOf[Integer with Test])
res0: Foo = Foo(10)
score:2
as @Travis Brown said This is a know issue,fixed in scala 2.11.7.
run under ammonite Repl 2.0.4 (scala 2.12.10 java 1.8.0_242)
@case class Foo(a: Int with Test)
a: Int with Test = 10
Source: stackoverflow.com
Related Query
- Case class companion object generation error for compound type
- Compile error when using a companion object of a case class as a type parameter
- Scala: order of definition for companion object vs case class
- How do I create an explicit companion object for a case class which behaves identically to the replaced compiler provided implicit companion object?
- Access companion object from case class (or vice-versa) using scala type macros
- No RowReaderFactory can be found for this type error when trying to map Cassandra row to case object using spark-cassandra-connector
- Generate companion object for case class with methods (field = method)
- Trouble creating a companion object for a case class in Scala
- Get companion object of class by given generic type Scala
- Why is the error "Unable to find encoder for type stored in a Dataset" when encoding JSON using case classes?
- Spark case class - decimal type encoder error "Cannot up cast from decimal"
- Case class and companion object
- In Scala, how can I define a companion object for a class defined in Java?
- Deriving circe Codec for a sealed case class family where base trait has a (sealed) type member
- Tupled method for case class having a type parameter
- generic trait taking a class and it's companion object as a type parameter
- Cannot find JsonWriter or JsonFormat type class for a case class
- Mapped projection with <> to a case class with companion object in Slick
- Scala Case Class Companion Objects - Conflict on the type name
- Deriving type class instances for case classes with exactly one field
- An object extends its companion case class in Scala
- Companion object in Scala isn't associating itself with case class
- automatically generate case object for case class
- What is the correct way to specify type variance for methods in a companion object
- LabelledGeneric instance generation on a tagged case class yields me an error in shapeless
- Why does SonarQube find this issue (<static initializer for >() uses a Side Effect Constructor) with case and object class files?
- Defining object for case class
- Implicit resolution for different orders of case class and companion
- Type mismatch when utilising a case class in a package object
- No TypeTag available for case class Type
More Query from same tag
- akka's Actor's receive method interaction with a Future (block) - can new messages come in before Future completes?
- How to copy uploaded file into the public folder?
- How to interpret this def that uses a PartialFunction?
- Scala Compilation Error : Value += is not member of Int
- sbt with sub-modules and multiple scala versions
- Jackson json deserialization into Seq[Double] but getting Integers instead of Doubles
- Scala Process exits but doesn't clean up threads
- Why does the empty string not match as Seq.empty?
- How to execute an action before start()?
- Need a way to save state in Play action
- Create Single sub Field from multiple Json Fields and apply to resulting Object using Play-json
- Enable Task Tags in Scala IDE for Eclipse
- Run configuration issue when running Scala in Eclipse for the first time
- How to dynamically cast a variable to Option
- importing scala classes into matlab?
- Reassign ArrayList in a better Way
- passing Dataframe contents into sql stored procedure
- sbt doesn't find class of managed dependency
- Exclude libraries that are marked as Provided using the sbt-osgi plugin
- sbt failure for java.sql.{Connector,DriverManager,ResultSet}
- curl, play & expect 100 continue header
- Converting a Spark's DataFrame column to List[String] in Scala
- How to produce nicely formatted XML in Scala?
- Calculate the list of the futures and and return the result future
- Strings concatenation in Spark SQL query
- split a record into individual records based of values of list, Scala
- Access SessionVar from LiftSession.onShutdownSession listener
- counting rows of a dataframe with condition in spark
- Scala: Implicit parameters should "without a prefix"?
- Akka Streams for server streaming (gRPC, Scala)