Is it possible easily turn off implicit int2long?
For example:
case class Id( v: Int ) extends AnyVal:
override def toString: String = v.toString
val id = Id(…)
val strId = id.toString
val idFromStr = Id(strId.toInt)
and now if i change ‘case class Id( v: Int )’ to ‘case class Id( v: Long )’ but don’t change ‘strId.toInt’ to ‘strId.toLong’ i may get runtime exception.
How force compiler warn me in situation when i change ‘id’ type not in all places?
Well you would get a runtime exception not because of int2long
but because String.toInt
is unsafe.
You can introduce two conversions from Int => Long
in scope , and that will lead to an ambiguous conversion erorr.
val x: Int = 1
given conv1: Conversion[Int, Long] = ???
given conv2: Conversion[Int, Long] = ???
x: Long // ERROR
/*
Found: (Playground.x : Int)
Required: Long
Note that implicit conversions cannot be applied because they are ambiguous;
both given instance conv1 in object Playground and given instance conv2 in object Playground convert from (Playground.x : Int) to Long
*/
See live example @ Scastie - An interactive playground for Scala.
I meant here: how force compiler warn me in situation when i change ‘id’ type not in all places.
Thank you for quick feedback.