Override Implicit Conversions Predef

I have several methods that return java.lang.Integer, which is then implicitly converted to Int using implicit Scala Predef transformations, here's how it is written:

implicit def Integer2int(x: java.lang.Integer): Int = x.intValue 

This conversion does not satisfy me, I would like something like:

 implicit def Integer2int(x: java.lang.Integer): Int = Option(x).getOrElse(new Integer(0)).intValue 

since Integer can sometimes be null , in which case the implicit Predef conversion also returns null, and I would like it to be 0. Instead

I wrote my own conversion, but I continue to get errors saying that this declaration is ambiguous, given that it is already declared in Predef.

My question is, is there a way to override implicit Predef conversions?

+6
source share
1 answer

You can disable Predef import as follows:

 import scala.Predef.{Integer2int => _} 

And then just override Integer2int as you wish.

Funny proof: http://ideone.com/R7Zyfd

+7
source

Source: https://habr.com/ru/post/969723/


All Articles