This page is no longer maintained — Please continue to the home page at www.scala-lang.org

how to handle implicit conversion ?

7 replies
david.bernard
Joined: 2009-01-08,
User offline. Last seen 1 year 27 weeks ago.

Hi,

I currently used a logging lib written in java
the java'method signature is
debug(String format, Object arg1)

I want to wrap the logger in a Richer api with support for more than
debug(...) method
So I wrote (simplify version)
debug(format : String, arg1 : Object) = wrapped.debug(format, arg1)

But the new code doesn't support logging int, ... (works with the java lib)

[WARNING] /MyFile.scala:19: error: type mismatch;
[WARNING] found : Int
[WARNING] required: java.lang.Object
[WARNING] Note that implicit conversions are not applicable because
they are ambiguous:
[WARNING] both method int2Integer in object Predef of type
(Int)java.lang.Integer
[WARNING] and method intWrapper in object Predef of type
(Int)scala.runtime.RichInt
[WARNING] are possible conversion functions from Int to java.lang.Object
[WARNING] log.debug("cacheMissing.size : {}", cacheMissing.size)

(user access the 'log' via mixin)
trait LogEnabled {
val log = new RichLogger(LoggerFactory.getLogger(this.getClass))
// var log = LoggerFactory.getLogger(this.getClass)
}

How can I fix the problem without modifying the existing calling code ?

/davidB

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: how to handle implicit conversion ?
Can't you make it:

[override] def debug(format : String, arg1 : Any) ?

On Mon, Jan 19, 2009 at 5:37 PM, David Bernard <david [dot] bernard [dot] 31 [at] gmail [dot] com> wrote:
..com">david [dot] bernard [dot] 31 [at] gmail [dot] com> wrote:
Hi,

I currently used a logging lib written in java
the java'method signature is
debug(String format, Object arg1)

I want to wrap the logger in a Richer api with support for more than
debug(...) method
So I wrote (simplify version)
debug(format : String, arg1 : Object) = wrapped.debug(format, arg1)

But the new code doesn't support logging int, ... (works with the java lib)

[WARNING] /MyFile.scala:19: error: type mismatch;
[WARNING]  found   : Int
[WARNING]  required: java.lang.Object
[WARNING] Note that implicit conversions are not applicable because
they are ambiguous:
[WARNING]  both method int2Integer in object Predef of type
(Int)java.lang.Integer
[WARNING]  and method intWrapper in object Predef of type
(Int)scala.runtime.RichInt
[WARNING]  are possible conversion functions from Int to java.lang.Object
[WARNING]     log.debug("cacheMissing.size : {}", cacheMissing.size)

(user access the 'log' via mixin)
trait LogEnabled {
 val log = new RichLogger(LoggerFactory.getLogger(this.getClass))
//  var log = LoggerFactory.getLogger(this.getClass)
}

How can I fix the problem without modifying the existing calling code ?

/davidB



--
Viktor Klang
Senior Systems Analyst
david.bernard
Joined: 2009-01-08,
User offline. Last seen 1 year 27 weeks ago.
Re: how to handle implicit conversion ?

No because, the backend logger is in java and want an array og Object

def trace(format : String, arg1 : Any, arg2 : Any, arg3 : Any) = if
(backend.isTraceEnabled) backend.trace(MessageFormatter.format(format,
Array(arg1, arg2, arg3)))

[WARNING] /home/dbernard/work/mimesis/playad/playad-utils/src/main/scala/com/playad/utils/LogEnabled.scala:107:
error: overloaded method value apply with alternatives
(Unit*)Array[Unit] (Double*)Array[Double]
(Float*)Array[Float] (Long*)Array[Long] (Int*)Array[Int]
(Char*)Array[Char] (Short*)Array[Short]
(Byte*)Array[Byte] (Boolean*)Array[Boolean] [A <:
AnyRef](A*)Array[A] cannot be applied to (Any,Any,Any,Any,Any)
[WARNING] def error(t : Throwable, format : String, arg1 : Any,
arg2 : Any, arg3 : Any, arg4 : Any, arg5 : Any) = if
(backend.isDebugEnabled) backend.error(MessageFormatter.format(format,
Array(arg1, arg2, arg3, arg4, arg5)), t)

And idem if I force the cast

[WARNING] /home/dbernard/work/mimesis/playad/playad-utils/src/main/scala/com/playad/utils/LogEnabled.scala:42:
error: overloaded method value apply with alternatives
(Unit*)Array[Unit] (Double*)Array[Double]
(Float*)Array[Float] (Long*)Array[Long] (Int*)Array[Int]
(Char*)Array[Char] (Short*)Array[Short]
(Byte*)Array[Byte] (Boolean*)Array[Boolean] [A <:
AnyRef](A*)Array[A] cannot be applied to (Any,Any,Any)
[WARNING] def trace(format : String, arg1 : Any, arg2 : Any, arg3 :
Any) = if (backend.isTraceEnabled)
backend.trace(MessageFormatter.format(format, Array(arg1, arg2,
arg3).asInstanceOf[Array[Object]]))

On Mon, Jan 19, 2009 at 17:44, Viktor Klang wrote:
> Can't you make it:
>
> [override] def debug(format : String, arg1 : Any) ?
>
> On Mon, Jan 19, 2009 at 5:37 PM, David Bernard
> wrote:
>>
>> Hi,
>>
>> I currently used a logging lib written in java
>> the java'method signature is
>> debug(String format, Object arg1)
>>
>> I want to wrap the logger in a Richer api with support for more than
>> debug(...) method
>> So I wrote (simplify version)
>> debug(format : String, arg1 : Object) = wrapped.debug(format, arg1)
>>
>> But the new code doesn't support logging int, ... (works with the java
>> lib)
>>
>> [WARNING] /MyFile.scala:19: error: type mismatch;
>> [WARNING] found : Int
>> [WARNING] required: java.lang.Object
>> [WARNING] Note that implicit conversions are not applicable because
>> they are ambiguous:
>> [WARNING] both method int2Integer in object Predef of type
>> (Int)java.lang.Integer
>> [WARNING] and method intWrapper in object Predef of type
>> (Int)scala.runtime.RichInt
>> [WARNING] are possible conversion functions from Int to java.lang.Object
>> [WARNING] log.debug("cacheMissing.size : {}", cacheMissing.size)
>>
>> (user access the 'log' via mixin)
>> trait LogEnabled {
>> val log = new RichLogger(LoggerFactory.getLogger(this.getClass))
>> // var log = LoggerFactory.getLogger(this.getClass)
>> }
>>
>> How can I fix the problem without modifying the existing calling code ?
>>
>> /davidB
>
>
>
> --
> Viktor Klang
> Senior Systems Analyst
>

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 3 days ago.
Re: how to handle implicit conversion ?
For your original question, try:

  def debug(format : String, arg : Any) = wrapped.debug(format, arg.asInstanceOf[AnyRef])

For your array question: Does the Java library expect an Array or a multiple parameter list? If it's multiple parameters, what version of Scala are you using?

If it's just an Array, you can do this:

  def trace(format : String, args : Any*) = if (backend.isTraceEnabled) backend.trace(MessageFormatter.format(format, args.map(_.asInstanceOf[AnyRef])))

If it's multiple parameters and you're using a recent version of Scala, replace:

  args.map(_.asInstanceOf[AnyRef])

with:

  args.map(_.asInstanceOf[AnyRef]) : _*

--j


--j

On Mon, Jan 19, 2009 at 8:50 AM, David Bernard <david [dot] bernard [dot] 31 [at] gmail [dot] com> wrote:
No because, the backend logger is in java and want an array og Object

 def trace(format : String, arg1 : Any,  arg2 : Any, arg3 : Any) = if
(backend.isTraceEnabled) backend.trace(MessageFormatter.format(format,
Array(arg1, arg2, arg3)))

[WARNING] /home/dbernard/work/mimesis/playad/playad-utils/src/main/scala/com/playad/utils/LogEnabled.scala:107:
error: overloaded method value apply with alternatives
(Unit*)Array[Unit] <and> (Double*)Array[Double] <and>
(Float*)Array[Float] <and> (Long*)Array[Long] <and> (Int*)Array[Int]
<and> (Char*)Array[Char] <and> (Short*)Array[Short] <and>
(Byte*)Array[Byte] <and> (Boolean*)Array[Boolean] <and> [A <:
AnyRef](A*)Array[A] cannot be applied to (Any,Any,Any,Any,Any)
[WARNING]   def error(t : Throwable, format : String, arg1 : Any,
arg2 : Any, arg3 : Any, arg4 : Any, arg5 : Any) = if
(backend.isDebugEnabled) backend.error(MessageFormatter.format(format,
Array(arg1, arg2, arg3, arg4, arg5)), t)

And idem if I force the cast

[WARNING] /home/dbernard/work/mimesis/playad/playad-utils/src/main/scala/com/playad/utils/LogEnabled.scala:42:
error: overloaded method value apply with alternatives
(Unit*)Array[Unit] <and> (Double*)Array[Double] <and>
(Float*)Array[Float] <and> (Long*)Array[Long] <and> (Int*)Array[Int]
<and> (Char*)Array[Char] <and> (Short*)Array[Short] <and>
(Byte*)Array[Byte] <and> (Boolean*)Array[Boolean] <and> [A <:
AnyRef](A*)Array[A] cannot be applied to (Any,Any,Any)
[WARNING]   def trace(format : String, arg1 : Any,  arg2 : Any, arg3 :
Any) = if (backend.isTraceEnabled)
backend.trace(MessageFormatter.format(format, Array(arg1, arg2,
arg3).asInstanceOf[Array[Object]]))




On Mon, Jan 19, 2009 at 17:44, Viktor Klang <viktor [dot] klang [at] gmail [dot] com> wrote:
> Can't you make it:
>
> [override] def debug(format : String, arg1 : Any) ?
>
> On Mon, Jan 19, 2009 at 5:37 PM, David Bernard <david [dot] bernard [dot] 31 [at] gmail [dot] com>
> wrote:
>>
>> Hi,
>>
>> I currently used a logging lib written in java
>> the java'method signature is
>> debug(String format, Object arg1)
>>
>> I want to wrap the logger in a Richer api with support for more than
>> debug(...) method
>> So I wrote (simplify version)
>> debug(format : String, arg1 : Object) = wrapped.debug(format, arg1)
>>
>> But the new code doesn't support logging int, ... (works with the java
>> lib)
>>
>> [WARNING] /MyFile.scala:19: error: type mismatch;
>> [WARNING]  found   : Int
>> [WARNING]  required: java.lang.Object
>> [WARNING] Note that implicit conversions are not applicable because
>> they are ambiguous:
>> [WARNING]  both method int2Integer in object Predef of type
>> (Int)java.lang.Integer
>> [WARNING]  and method intWrapper in object Predef of type
>> (Int)scala.runtime.RichInt
>> [WARNING]  are possible conversion functions from Int to java.lang.Object
..lang.Object
>> [WARNING]     log.debug("cacheMissing.size : {}", cacheMissing.size)
>>
>> (user access the 'log' via mixin)
>> trait LogEnabled {
>>  val log = new RichLogger(LoggerFactory.getLogger(this.getClass))
>> //  var log = LoggerFactory.getLogger(this.getClass)
>> }
>>
>> How can I fix the problem without modifying the existing calling code ?
>>
>> /davidB
>
>
>
> --
> Viktor Klang
> Senior Systems Analyst
>

david.bernard
Joined: 2009-01-08,
User offline. Last seen 1 year 27 weeks ago.
Re: how to handle implicit conversion ?

No it doesn't work :

scala> def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object])))
def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object])))
trace: (String,Any*)Unit

scala> trace("hello {} {}", 3, "foo")
trace("hello {} {}", 3, "foo")
hello Array(3, foo) {}

because it's the wrong method of MessageFormatter that is called (the
first instead of the third):
public static final String format(String messagePattern, Object arg)
public static final String format(String messagePattern, Object
arg1, Object arg2)
public static final String arrayFormat(String messagePattern,
Object[] argArray)

currently form my scala code I cast the array to access the right
method. It's to avoid this repetitive cast/pattern that I try to wrote
a RichLogger
If I try to apply this cast

scala> def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object]).asInstanceOf[Array[Object]]))
def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object]).asInstanceOf[Array[Object]]))
trace: (String,Any*)Unit

scala> trace("hello {} {}", 3, "foo")
trace("hello {} {}", 3, "foo")
java.lang.ClassCastException: scala.runtime.BoxedAnyArray cannot be
cast to [Ljava.lang.Object;
at .trace(:5)
at .(:7)
at .()
at RequestResult$.(:3)
at RequestResult$.()
at RequestResult$result()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImp...

On Mon, Jan 19, 2009 at 18:32, Jorge Ortiz wrote:
> For your original question, try:
>
> def debug(format : String, arg : Any) = wrapped.debug(format,
> arg.asInstanceOf[AnyRef])
>
> For your array question: Does the Java library expect an Array or a multiple
> parameter list? If it's multiple parameters, what version of Scala are you
> using?
>
> If it's just an Array, you can do this:
>
> def trace(format : String, args : Any*) = if (backend.isTraceEnabled)
> backend.trace(MessageFormatter.format(format,
> args.map(_.asInstanceOf[AnyRef])))
>
> If it's multiple parameters and you're using a recent version of Scala,
> replace:
>
> args.map(_.asInstanceOf[AnyRef])
>
> with:
>
> args.map(_.asInstanceOf[AnyRef]) : _*
>
> --j
>
> On Mon, Jan 19, 2009 at 8:50 AM, David Bernard
> wrote:
>>
>> No because, the backend logger is in java and want an array og Object
>>
>> def trace(format : String, arg1 : Any, arg2 : Any, arg3 : Any) = if
>> (backend.isTraceEnabled) backend.trace(MessageFormatter.format(format,
>> Array(arg1, arg2, arg3)))
>>
>> [WARNING]
>> /home/dbernard/work/mimesis/playad/playad-utils/src/main/scala/com/playad/utils/LogEnabled.scala:107:
>> error: overloaded method value apply with alternatives
>> (Unit*)Array[Unit] (Double*)Array[Double]
>> (Float*)Array[Float] (Long*)Array[Long] (Int*)Array[Int]
>> (Char*)Array[Char] (Short*)Array[Short]
>> (Byte*)Array[Byte] (Boolean*)Array[Boolean] [A <:
>> AnyRef](A*)Array[A] cannot be applied to (Any,Any,Any,Any,Any)
>> [WARNING] def error(t : Throwable, format : String, arg1 : Any,
>> arg2 : Any, arg3 : Any, arg4 : Any, arg5 : Any) = if
>> (backend.isDebugEnabled) backend.error(MessageFormatter.format(format,
>> Array(arg1, arg2, arg3, arg4, arg5)), t)
>>
>> And idem if I force the cast
>>
>> [WARNING]
>> /home/dbernard/work/mimesis/playad/playad-utils/src/main/scala/com/playad/utils/LogEnabled.scala:42:
>> error: overloaded method value apply with alternatives
>> (Unit*)Array[Unit] (Double*)Array[Double]
>> (Float*)Array[Float] (Long*)Array[Long] (Int*)Array[Int]
>> (Char*)Array[Char] (Short*)Array[Short]
>> (Byte*)Array[Byte] (Boolean*)Array[Boolean] [A <:
>> AnyRef](A*)Array[A] cannot be applied to (Any,Any,Any)
>> [WARNING] def trace(format : String, arg1 : Any, arg2 : Any, arg3 :
>> Any) = if (backend.isTraceEnabled)
>> backend.trace(MessageFormatter.format(format, Array(arg1, arg2,
>> arg3).asInstanceOf[Array[Object]]))
>>
>>
>>
>>
>> On Mon, Jan 19, 2009 at 17:44, Viktor Klang
>> wrote:
>> > Can't you make it:
>> >
>> > [override] def debug(format : String, arg1 : Any) ?
>> >
>> > On Mon, Jan 19, 2009 at 5:37 PM, David Bernard
>> >
>> > wrote:
>> >>
>> >> Hi,
>> >>
>> >> I currently used a logging lib written in java
>> >> the java'method signature is
>> >> debug(String format, Object arg1)
>> >>
>> >> I want to wrap the logger in a Richer api with support for more than
>> >> debug(...) method
>> >> So I wrote (simplify version)
>> >> debug(format : String, arg1 : Object) = wrapped.debug(format, arg1)
>> >>
>> >> But the new code doesn't support logging int, ... (works with the java
>> >> lib)
>> >>
>> >> [WARNING] /MyFile.scala:19: error: type mismatch;
>> >> [WARNING] found : Int
>> >> [WARNING] required: java.lang.Object
>> >> [WARNING] Note that implicit conversions are not applicable because
>> >> they are ambiguous:
>> >> [WARNING] both method int2Integer in object Predef of type
>> >> (Int)java.lang.Integer
>> >> [WARNING] and method intWrapper in object Predef of type
>> >> (Int)scala.runtime.RichInt
>> >> [WARNING] are possible conversion functions from Int to
>> >> java.lang.Object
>> >> [WARNING] log.debug("cacheMissing.size : {}", cacheMissing.size)
>> >>
>> >> (user access the 'log' via mixin)
>> >> trait LogEnabled {
>> >> val log = new RichLogger(LoggerFactory.getLogger(this.getClass))
>> >> // var log = LoggerFactory.getLogger(this.getClass)
>> >> }
>> >>
>> >> How can I fix the problem without modifying the existing calling code ?
>> >>
>> >> /davidB
>> >
>> >
>> >
>> > --
>> > Viktor Klang
>> > Senior Systems Analyst
>> >
>
>

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 3 days ago.
Re: how to handle implicit conversion ?
Try the following:

scala> def trace(format: String, args: Any*) = MessageFormatter.arrayFormat(format, args.map(_.asInstanceOf[AnyRef]).toArray)                           
def trace(format: String, args: Any*) = MessageFormatter.arrayFormat(format, args.map(_.asInstanceOf[AnyRef]).toArray)
trace: (String,Any*)java.lang.String

scala> trace("hello {} {}", 3, "foo")                                           trace("hello {} {}", 3, "foo")              
res8: java.lang.String = hello 3 foo


--j

On Tue, Jan 20, 2009 at 2:19 AM, David Bernard <david [dot] bernard [dot] 31 [at] gmail [dot] com> wrote:
No it doesn't work :

scala> def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object])))
def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object])))
trace: (String,Any*)Unit

scala> trace("hello {} {}", 3, "foo")
trace("hello {} {}", 3, "foo")
hello Array(3, foo) {}

because it's the wrong method of MessageFormatter that is called (the
first instead of the third):
 public static final String format(String messagePattern, Object arg)
 public static final String format(String messagePattern, Object
arg1, Object arg2)
 public static final String arrayFormat(String messagePattern,
Object[] argArray)

currently form my scala code I cast the array to access the right
method. It's to avoid this repetitive cast/pattern that I try to wrote
a RichLogger
If I try to apply this cast

scala> def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object]).asInstanceOf[Array[Object]]))
def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object]).asInstanceOf[Array[Object]]))
trace: (String,Any*)Unit

scala> trace("hello {} {}", 3, "foo")
trace("hello {} {}", 3, "foo")
java.lang.ClassCastException: scala.runtime.BoxedAnyArray cannot be
cast to [Ljava.lang.Object;
       at .trace(<console>:5)
       at .<init>(<console>:7)
       at .<clinit>(<console>)
       at RequestResult$.<init>(<console>:3)
       at RequestResult$.<clinit>(<console>)
       at RequestResult$result(<console>)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImp...


On Mon, Jan 19, 2009 at 18:32, Jorge Ortiz <jorge [dot] ortiz [at] gmail [dot] com> wrote:
> For your original question, try:
>
>   def debug(format : String, arg : Any) = wrapped.debug(format,
> arg.asInstanceOf[AnyRef])
>
> For your array question: Does the Java library expect an Array or a multiple
> parameter list? If it's multiple parameters, what version of Scala are you
> using?
>
> If it's just an Array, you can do this:
>
>   def trace(format : String, args : Any*) = if (backend.isTraceEnabled)
> backend.trace(MessageFormatter.format(format,
> args.map(_.asInstanceOf[AnyRef])))
>
> If it's multiple parameters and you're using a recent version of Scala,
> replace:
>
>   args.map(_.asInstanceOf[AnyRef])
>
> with:
>
>   args.map(_.asInstanceOf[AnyRef]) : _*
>
> --j
>
> On Mon, Jan 19, 2009 at 8:50 AM, David Bernard <david [dot] bernard [dot] 31 [at] gmail [dot] com>
> wrote:
>>
>> No because, the backend logger is in java and want an array og Object
>>
>>  def trace(format : String, arg1 : Any,  arg2 : Any, arg3 : Any) = if
>> (backend.isTraceEnabled) backend.trace(MessageFormatter.format(format,
>> Array(arg1, arg2, arg3)))
>>
>> [WARNING]
>> /home/dbernard/work/mimesis/playad/playad-utils/src/main/scala/com/playad/utils/LogEnabled.scala:107:
>> error: overloaded method value apply with alternatives
>> (Unit*)Array[Unit] <and> (Double*)Array[Double] <and>
>> (Float*)Array[Float] <and> (Long*)Array[Long] <and> (Int*)Array[Int]
>> <and> (Char*)Array[Char] <and> (Short*)Array[Short] <and>
>> (Byte*)Array[Byte] <and> (Boolean*)Array[Boolean] <and> [A <:
>> AnyRef](A*)Array[A] cannot be applied to (Any,Any,Any,Any,Any)
>> [WARNING]   def error(t : Throwable, format : String, arg1 : Any,
>> arg2 : Any, arg3 : Any, arg4 : Any, arg5 : Any) = if
>> (backend.isDebugEnabled) backend.error(MessageFormatter.format(format,
>> Array(arg1, arg2, arg3, arg4, arg5)), t)
>>
>> And idem if I force the cast
>>
>> [WARNING]
>> /home/dbernard/work/mimesis/playad/playad-utils/src/main/scala/com/playad/utils/LogEnabled.scala:42:
>> error: overloaded method value apply with alternatives
>> (Unit*)Array[Unit] <and> (Double*)Array[Double] <and>
>> (Float*)Array[Float] <and> (Long*)Array[Long] <and> (Int*)Array[Int]
>> <and> (Char*)Array[Char] <and> (Short*)Array[Short] <and>
>> (Byte*)Array[Byte] <and> (Boolean*)Array[Boolean] <and> [A <:
>> AnyRef](A*)Array[A] cannot be applied to (Any,Any,Any)
>> [WARNING]   def trace(format : String, arg1 : Any,  arg2 : Any, arg3 :
>> Any) = if (backend.isTraceEnabled)
>> backend.trace(MessageFormatter.format(format, Array(arg1, arg2,
>> arg3).asInstanceOf[Array[Object]]))
>>
>>
>>
>>
>> On Mon, Jan 19, 2009 at 17:44, Viktor Klang <viktor [dot] klang [at] gmail [dot] com>
>> wrote:
>> > Can't you make it:
>> >
>> > [override] def debug(format : String, arg1 : Any) ?
>> >
>> > On Mon, Jan 19, 2009 at 5:37 PM, David Bernard
>> > <david [dot] bernard [dot] 31 [at] gmail [dot] com>
>> > wrote:
>> >>
>> >> Hi,
>> >>
>> >> I currently used a logging lib written in java
>> >> the java'method signature is
>> >> debug(String format, Object arg1)
>> >>
>> >> I want to wrap the logger in a Richer api with support for more than
>> >> debug(...) method
>> >> So I wrote (simplify version)
>> >> debug(format : String, arg1 : Object) = wrapped.debug(format, arg1)
>> >>
>> >> But the new code doesn't support logging int, ... (works with the java
>> >> lib)
>> >>
>> >> [WARNING] /MyFile.scala:19: error: type mismatch;
>> >> [WARNING]  found   : Int
>> >> [WARNING]  required: java.lang.Object
>> >> [WARNING] Note that implicit conversions are not applicable because
>> >> they are ambiguous:
>> >> [WARNING]  both method int2Integer in object Predef of type
>> >> (Int)java.lang.Integer
>> >> [WARNING]  and method intWrapper in object Predef of type
>> >> (Int)scala.runtime.RichInt
>> >> [WARNING]  are possible conversion functions from Int to
>> >> java.lang.Object
>> >> [WARNING]     log.debug("cacheMissing.size : {}", cacheMissing.size)
>> >>
>> >> (user access the 'log' via mixin)
>> >> trait LogEnabled {
>> >>  val log = new RichLogger(LoggerFactory.getLogger(this.getClass))
>> >> //  var log = LoggerFactory.getLogger(this.getClass)
>> >> }
>> >>
>> >> How can I fix the problem without modifying the existing calling code ?
>> >>
>> >> /davidB
>> >
>> >
>> >
>> > --
>> > Viktor Klang
>> > Senior Systems Analyst
>> >
>
>

david.bernard
Joined: 2009-01-08,
User offline. Last seen 1 year 27 weeks ago.
Re: how to handle implicit conversion ?
Thanks Jorge,

but the method is for logging (could be use in video pipeline), so (micro) performance are important => I avoid varargs and create severals method with 0 to 5 args
And my first adaptation to your code gave

scala> def trace(format : String, arg1: Any, arg2 : Any) = MessageFormatter.arrayFormat(format, Array(arg1, arg2).mapdef trace(format : String, arg1: Any, arg2 : Any) = MessageFormatter.arrayFormat(format, Array(arg1, arg2).map(_.asInstanceOf[AnyRef]).asInstanceOf[Array[AnyRef]].toArray)
def trace(format : String, arg1: Any, arg2 : Any) = MessageFormatter.arrayFormat(format, Array(arg1, arg2).map(_.asInstanceOf[AnyRef]).asInstanceOf[Array[AnyRef]].toArray)
<console>:5: error: overloaded method value apply with alternatives (Unit*)Array[Unit] <and> (Double*)Array[Double] <and> (Float*)Array[Float] <and> (Long*)Array[Long] <and> (Int*)Array[Int] <and> (Char*)Array[Char] <and> (Short*)Array[Short] <and> (Byte*)Array[Byte] <and> (Boolean*)Array[Boolean] <and> [A <: AnyRef](A*)Array[A] cannot be applied to (Any,Any)
       def trace(format : String, arg1: Any, arg2 : Any) = MessageFormatter.arrayFormat(format, Array(arg1, arg2).map(_.asInstanceOf[AnyRef]).asInstanceOf[Array[AnyRef]].toArray)

But if I did the "map" directly it works :

scala> def trace(format : String, arg1: Any, arg2 : Any) = MessageFormatter.arrayFormat(format, Array(arg1.asInstanceOf[AnyRef], arg2.asInstanceOf[AnyRef]).toArray)
def trace(format : String, arg1: Any, arg2 : Any) = MessageFormatter.arrayFormat(format, Array(arg1.asInstanceOf[AnyRef], arg2.asInstanceOf[AnyRef]).toArray)
trace: (String,Any,Any)java.lang.String

scala> trace("hello {} {}", "hello" ,33)
trace("hello {} {}", "hello" ,33)
res0: java.lang.String = hello hello 33

Thanks

On Tue, Jan 20, 2009 at 19:43, Jorge Ortiz <jorge [dot] ortiz [at] gmail [dot] com> wrote:
Try the following:

scala> def trace(format: String, args: Any*) = MessageFormatter.arrayFormat(format, args.map(_.asInstanceOf[AnyRef]).toArray)                           
def trace(format: String, args: Any*) = MessageFormatter.arrayFormat(format, args.map(_.asInstanceOf[AnyRef]).toArray)
trace: (String,Any*)java.lang.String

scala> trace("hello {} {}", 3, "foo")                                           trace("hello {} {}", 3, "foo")              
res8: java.lang.String = hello 3 foo


--j

On Tue, Jan 20, 2009 at 2:19 AM, David Bernard <david [dot] bernard [dot] 31 [at] gmail [dot] com> wrote:
No it doesn't work :

scala> def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object])))
def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object])))
trace: (String,Any*)Unit

scala> trace("hello {} {}", 3, "foo")
trace("hello {} {}", 3, "foo")
hello Array(3, foo) {}

because it's the wrong method of MessageFormatter that is called (the
first instead of the third):
 public static final String format(String messagePattern, Object arg)
 public static final String format(String messagePattern, Object
arg1, Object arg2)
 public static final String arrayFormat(String messagePattern,
Object[] argArray)

currently form my scala code I cast the array to access the right
method. It's to avoid this repetitive cast/pattern that I try to wrote
a RichLogger
If I try to apply this cast

scala> def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object]).asInstanceOf[Array[Object]]))
def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object]).asInstanceOf[Array[Object]]))
trace: (String,Any*)Unit

scala> trace("hello {} {}", 3, "foo")
trace("hello {} {}", 3, "foo")
java.lang.ClassCastException: scala.runtime.BoxedAnyArray cannot be
cast to [Ljava.lang.Object;
       at .trace(<console>:5)
       at .<init>(<console>:7)
       at .<clinit>(<console>)
       at RequestResult$.<init>(<console>:3)
       at RequestResult$.<clinit>(<console>)
       at RequestResult$result(<console>)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImp...


On Mon, Jan 19, 2009 at 18:32, Jorge Ortiz <jorge [dot] ortiz [at] gmail [dot] com> wrote:
> For your original question, try:
>
>   def debug(format : String, arg : Any) = wrapped.debug(format,
> arg.asInstanceOf[AnyRef])
>
> For your array question: Does the Java library expect an Array or a multiple
> parameter list? If it's multiple parameters, what version of Scala are you
> using?
>
> If it's just an Array, you can do this:
>
>   def trace(format : String, args : Any*) = if (backend.isTraceEnabled)
> backend.trace(MessageFormatter.format(format,
> args.map(_.asInstanceOf[AnyRef])))
>
> If it's multiple parameters and you're using a recent version of Scala,
> replace:
>
>   args.map(_.asInstanceOf[AnyRef])
>
> with:
>
>   args.map(_.asInstanceOf[AnyRef]) : _*
>
> --j
>
> On Mon, Jan 19, 2009 at 8:50 AM, David Bernard <david [dot] bernard [dot] 31 [at] gmail [dot] com>
> wrote:
>>
>> No because, the backend logger is in java and want an array og Object
>>
>>  def trace(format : String, arg1 : Any,  arg2 : Any, arg3 : Any) = if
>> (backend.isTraceEnabled) backend.trace(MessageFormatter.format(format,
>> Array(arg1, arg2, arg3)))
>>
>> [WARNING]
>> /home/dbernard/work/mimesis/playad/playad-utils/src/main/scala/com/playad/utils/LogEnabled.scala:107:
>> error: overloaded method value apply with alternatives
>> (Unit*)Array[Unit] <and> (Double*)Array[Double] <and>
>> (Float*)Array[Float] <and> (Long*)Array[Long] <and> (Int*)Array[Int]
>> <and> (Char*)Array[Char] <and> (Short*)Array[Short] <and>
>> (Byte*)Array[Byte] <and> (Boolean*)Array[Boolean] <and> [A <:
>> AnyRef](A*)Array[A] cannot be applied to (Any,Any,Any,Any,Any)
>> [WARNING]   def error(t : Throwable, format : String, arg1 : Any,
>> arg2 : Any, arg3 : Any, arg4 : Any, arg5 : Any) = if
>> (backend.isDebugEnabled) backend.error(MessageFormatter.format(format,
>> Array(arg1, arg2, arg3, arg4, arg5)), t)
>>
>> And idem if I force the cast
>>
>> [WARNING]
>> /home/dbernard/work/mimesis/playad/playad-utils/src/main/scala/com/playad/utils/LogEnabled.scala:42:
>> error: overloaded method value apply with alternatives
>> (Unit*)Array[Unit] <and> (Double*)Array[Double] <and>
>> (Float*)Array[Float] <and> (Long*)Array[Long] <and> (Int*)Array[Int]
>> <and> (Char*)Array[Char] <and> (Short*)Array[Short] <and>
>> (Byte*)Array[Byte] <and> (Boolean*)Array[Boolean] <and> [A <:
>> AnyRef](A*)Array[A] cannot be applied to (Any,Any,Any)
>> [WARNING]   def trace(format : String, arg1 : Any,  arg2 : Any, arg3 :
>> Any) = if (backend.isTraceEnabled)
>> backend.trace(MessageFormatter.format(format, Array(arg1, arg2,
>> arg3).asInstanceOf[Array[Object]]))
>>
>>
>>
>>
>> On Mon, Jan 19, 2009 at 17:44, Viktor Klang <viktor [dot] klang [at] gmail [dot] com>
>> wrote:
>> > Can't you make it:
>> >
>> > [override] def debug(format : String, arg1 : Any) ?
>> >
>> > On Mon, Jan 19, 2009 at 5:37 PM, David Bernard
>> > <david [dot] bernard [dot] 31 [at] gmail [dot] com>
>> > wrote:
>> >>
>> >> Hi,
>> >>
>> >> I currently used a logging lib written in java
>> >> the java'method signature is
>> >> debug(String format, Object arg1)
>> >>
>> >> I want to wrap the logger in a Richer api with support for more than
>> >> debug(...) method
>> >> So I wrote (simplify version)
>> >> debug(format : String, arg1 : Object) = wrapped.debug(format, arg1)
>> >>
>> >> But the new code doesn't support logging int, ... (works with the java
>> >> lib)
>> >>
>> >> [WARNING] /MyFile.scala:19: error: type mismatch;
>> >> [WARNING]  found   : Int
>> >> [WARNING]  required: java.lang.Object
>> >> [WARNING] Note that implicit conversions are not applicable because
>> >> they are ambiguous:
>> >> [WARNING]  both method int2Integer in object Predef of type
>> >> (Int)java.lang.Integer
>> >> [WARNING]  and method intWrapper in object Predef of type
>> >> (Int)scala.runtime.RichInt
>> >> [WARNING]  are possible conversion functions from Int to
>> >> java.lang.Object
>> >> [WARNING]     log.debug("cacheMissing.size : {}", cacheMissing.size)
>> >>
>> >> (user access the 'log' via mixin)
>> >> trait LogEnabled {
>> >>  val log = new RichLogger(LoggerFactory.getLogger(this.getClass))
>> >> //  var log = LoggerFactory.getLogger(this.getClass)
>> >> }
>> >>
>> >> How can I fix the problem without modifying the existing calling code ?
>> >>
>> >> /davidB
>> >
>> >
>> >
>> > --
>> > Viktor Klang
>> > Senior Systems Analyst
>> >
>
>


david.bernard
Joined: 2009-01-08,
User offline. Last seen 1 year 27 weeks ago.
Re: how to handle implicit conversion ?
Quick note, the last .toArray is useless :

def trace(format : String, arg1: Any, arg2 : Any) = MessageFormatter.arrayFormat(format, Array(arg1.asInstanceOf[AnyRef], arg2.asInstanceOf[AnyRef]))

works

Thanks for your helps

On Wed, Jan 21, 2009 at 10:06, David Bernard <david [dot] bernard [dot] 31 [at] gmail [dot] com> wrote:
Thanks Jorge,

but the method is for logging (could be use in video pipeline), so (micro) performance are important => I avoid varargs and create severals method with 0 to 5 args
And my first adaptation to your code gave

scala> def trace(format : String, arg1: Any, arg2 : Any) = MessageFormatter.arrayFormat(format, Array(arg1, arg2).mapdef trace(format : String, arg1: Any, arg2 : Any) = MessageFormatter.arrayFormat(format, Array(arg1, arg2).map(_.asInstanceOf[AnyRef]).asInstanceOf[Array[AnyRef]].toArray)
def trace(format : String, arg1: Any, arg2 : Any) = MessageFormatter.arrayFormat(format, Array(arg1, arg2).map(_.asInstanceOf[AnyRef]).asInstanceOf[Array[AnyRef]].toArray)
<console>:5: error: overloaded method value apply with alternatives (Unit*)Array[Unit] <and> (Double*)Array[Double] <and> (Float*)Array[Float] <and> (Long*)Array[Long] <and> (Int*)Array[Int] <and> (Char*)Array[Char] <and> (Short*)Array[Short] <and> (Byte*)Array[Byte] <and> (Boolean*)Array[Boolean] <and> [A <: AnyRef](A*)Array[A] cannot be applied to (Any,Any)
       def trace(format : String, arg1: Any, arg2 : Any) = MessageFormatter.arrayFormat(format, Array(arg1, arg2).map(_.asInstanceOf[AnyRef]).asInstanceOf[Array[AnyRef]].toArray)

But if I did the "map" directly it works :

scala> def trace(format : String, arg1: Any, arg2 : Any) = MessageFormatter.arrayFormat(format, Array(arg1.asInstanceOf[AnyRef], arg2.asInstanceOf[AnyRef]).toArray)
def trace(format : String, arg1: Any, arg2 : Any) = MessageFormatter.arrayFormat(format, Array(arg1.asInstanceOf[AnyRef], arg2.asInstanceOf[AnyRef]).toArray)
trace: (String,Any,Any)java.lang.String

scala> trace("hello {} {}", "hello" ,33)
trace("hello {} {}", "hello" ,33)
res0: java.lang.String = hello hello 33

Thanks

On Tue, Jan 20, 2009 at 19:43, Jorge Ortiz <jorge [dot] ortiz [at] gmail [dot] com> wrote:
Try the following:

scala> def trace(format: String, args: Any*) = MessageFormatter.arrayFormat(format, args.map(_.asInstanceOf[AnyRef]).toArray)                           
def trace(format: String, args: Any*) = MessageFormatter.arrayFormat(format, args.map(_.asInstanceOf[AnyRef]).toArray)
trace: (String,Any*)java.lang.String

scala> trace("hello {} {}", 3, "foo")                                           trace("hello {} {}", 3, "foo")              
res8: java.lang.String = hello 3 foo


--j

On Tue, Jan 20, 2009 at 2:19 AM, David Bernard <david [dot] bernard [dot] 31 [at] gmail [dot] com> wrote:
No it doesn't work :

scala> def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object])))
def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object])))
trace: (String,Any*)Unit

scala> trace("hello {} {}", 3, "foo")
trace("hello {} {}", 3, "foo")
hello Array(3, foo) {}

because it's the wrong method of MessageFormatter that is called (the
first instead of the third):
 public static final String format(String messagePattern, Object arg)
 public static final String format(String messagePattern, Object
arg1, Object arg2)
 public static final String arrayFormat(String messagePattern,
Object[] argArray)

currently form my scala code I cast the array to access the right
method. It's to avoid this repetitive cast/pattern that I try to wrote
a RichLogger
If I try to apply this cast

scala> def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object]).asInstanceOf[Array[Object]]))
def trace(format : String, args: Any*) =
println(MessageFormatter.format(format,
args.map(_.asInstanceOf[Object]).asInstanceOf[Array[Object]]))
trace: (String,Any*)Unit

scala> trace("hello {} {}", 3, "foo")
trace("hello {} {}", 3, "foo")
java.lang.ClassCastException: scala.runtime.BoxedAnyArray cannot be
cast to [Ljava.lang.Object;
       at .trace(<console>:5)
       at .<init>(<console>:7)
       at .<clinit>(<console>)
       at RequestResult$.<init>(<console>:3)
       at RequestResult$.<clinit>(<console>)
       at RequestResult$result(<console>)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImp...


On Mon, Jan 19, 2009 at 18:32, Jorge Ortiz <jorge [dot] ortiz [at] gmail [dot] com> wrote:
> For your original question, try:
>
>   def debug(format : String, arg : Any) = wrapped.debug(format,
> arg.asInstanceOf[AnyRef])
>
> For your array question: Does the Java library expect an Array or a multiple
> parameter list? If it's multiple parameters, what version of Scala are you
> using?
>
> If it's just an Array, you can do this:
>
>   def trace(format : String, args : Any*) = if (backend.isTraceEnabled)
> backend.trace(MessageFormatter.format(format,
> args.map(_.asInstanceOf[AnyRef])))
>
> If it's multiple parameters and you're using a recent version of Scala,
> replace:
>
>   args.map(_.asInstanceOf[AnyRef])
>
> with:
>
>   args.map(_.asInstanceOf[AnyRef]) : _*
>
> --j
>
> On Mon, Jan 19, 2009 at 8:50 AM, David Bernard <david [dot] bernard [dot] 31 [at] gmail [dot] com>
> wrote:
>>
>> No because, the backend logger is in java and want an array og Object
>>
>>  def trace(format : String, arg1 : Any,  arg2 : Any, arg3 : Any) = if
>> (backend.isTraceEnabled) backend.trace(MessageFormatter.format(format,
>> Array(arg1, arg2, arg3)))
>>
>> [WARNING]
>> /home/dbernard/work/mimesis/playad/playad-utils/src/main/scala/com/playad/utils/LogEnabled.scala:107:
>> error: overloaded method value apply with alternatives
>> (Unit*)Array[Unit] <and> (Double*)Array[Double] <and>
>> (Float*)Array[Float] <and> (Long*)Array[Long] <and> (Int*)Array[Int]
>> <and> (Char*)Array[Char] <and> (Short*)Array[Short] <and>
>> (Byte*)Array[Byte] <and> (Boolean*)Array[Boolean] <and> [A <:
>> AnyRef](A*)Array[A] cannot be applied to (Any,Any,Any,Any,Any)
>> [WARNING]   def error(t : Throwable, format : String, arg1 : Any,
>> arg2 : Any, arg3 : Any, arg4 : Any, arg5 : Any) = if
>> (backend.isDebugEnabled) backend.error(MessageFormatter.format(format,
>> Array(arg1, arg2, arg3, arg4, arg5)), t)
>>
>> And idem if I force the cast
>>
>> [WARNING]
>> /home/dbernard/work/mimesis/playad/playad-utils/src/main/scala/com/playad/utils/LogEnabled.scala:42:
>> error: overloaded method value apply with alternatives
>> (Unit*)Array[Unit] <and> (Double*)Array[Double] <and>
>> (Float*)Array[Float] <and> (Long*)Array[Long] <and> (Int*)Array[Int]
>> <and> (Char*)Array[Char] <and> (Short*)Array[Short] <and>
>> (Byte*)Array[Byte] <and> (Boolean*)Array[Boolean] <and> [A <:
>> AnyRef](A*)Array[A] cannot be applied to (Any,Any,Any)
>> [WARNING]   def trace(format : String, arg1 : Any,  arg2 : Any, arg3 :
>> Any) = if (backend.isTraceEnabled)
>> backend.trace(MessageFormatter.format(format, Array(arg1, arg2,
>> arg3).asInstanceOf[Array[Object]]))
>>
>>
>>
>>
>> On Mon, Jan 19, 2009 at 17:44, Viktor Klang <viktor [dot] klang [at] gmail [dot] com>
>> wrote:
>> > Can't you make it:
>> >
>> > [override] def debug(format : String, arg1 : Any) ?
>> >
>> > On Mon, Jan 19, 2009 at 5:37 PM, David Bernard
>> > <david [dot] bernard [dot] 31 [at] gmail [dot] com>
>> > wrote:
>> >>
>> >> Hi,
>> >>
>> >> I currently used a logging lib written in java
>> >> the java'method signature is
>> >> debug(String format, Object arg1)
>> >>
>> >> I want to wrap the logger in a Richer api with support for more than
>> >> debug(...) method
>> >> So I wrote (simplify version)
>> >> debug(format : String, arg1 : Object) = wrapped.debug(format, arg1)
>> >>
>> >> But the new code doesn't support logging int, ... (works with the java
>> >> lib)
>> >>
>> >> [WARNING] /MyFile.scala:19: error: type mismatch;
>> >> [WARNING]  found   : Int
>> >> [WARNING]  required: java.lang.Object
>> >> [WARNING] Note that implicit conversions are not applicable because
>> >> they are ambiguous:
>> >> [WARNING]  both method int2Integer in object Predef of type
>> >> (Int)java.lang.Integer
>> >> [WARNING]  and method intWrapper in object Predef of type
>> >> (Int)scala.runtime.RichInt
>> >> [WARNING]  are possible conversion functions from Int to
>> >> java.lang.Object
>> >> [WARNING]     log.debug("cacheMissing.size : {}", cacheMissing.size)
>> >>
>> >> (user access the 'log' via mixin)
>> >> trait LogEnabled {
>> >>  val log = new RichLogger(LoggerFactory.getLogger(this.getClass))
>> >> //  var log = LoggerFactory.getLogger(this.getClass)
>> >> }
>> >>
>> >> How can I fix the problem without modifying the existing calling code ?
>> >>
>> >> /davidB
>> >
>> >
>> >
>> > --
>> > Viktor Klang
>> > Senior Systems Analyst
>> >
>
>



Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland