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

@specialized and <

2 replies
arya
Joined: 2010-02-11,
User offline. Last seen 24 weeks 6 days ago.

So I've got

def foo1[T](a:T,b:T) = a < b

But T doesn't provide < , so:

def foo2[T <: Ordered[T]](a:T,b:T) = a < b

but I can't call foo2(3,4) because Int doesn't provide Ordered[Int], so

def foo3[T <% Ordered[T]](a:T,b:T) = a < b

It finally compiles! But I want a @specialized version for Int, so

def foo4[@specialized("Int") T <% Ordered[T]](a:T,b:T) = a < b

but it still boxes the Int, does a cast check, and then calls Ordered.less:

public boolean foo4$mIc$sp(int, int, scala.Function1);
Code:
0: aload_3
1: iload_1
2: invokestatic #27; //Method
scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
5: invokeinterface #33, 2; //InterfaceMethod
scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;
10: checkcast #35; //class scala/math/Ordered
13: iload_2
14: invokestatic #27; //Method
scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
17: invokeinterface #39, 2; //InterfaceMethod
scala/math/Ordered.$less:(Ljava/lang/Object;)Z
22: ireturn

Any suggestions?

Thanks,
Arya

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: @specialized and <

This was discussed recently [1, 2] in the context of Numeric. The
specialization occurs too late in the compilation phases to avoid the
boxing and indirection.

I envisioned an 'early phase' specialisation that would occur before
the 'typer' phase. Others contended that the combination sof
specializing other classes (in your case, Ordered), using @inline, and
relying on Hotspot, the overhead could be eliminated anyway.

-jason

[1] http://old.nabble.com/Early-Phase-%40specialized-td27173135.html#a27173135
[2] http://old.nabble.com/Practical-limits-of-generalization:-Was:-Early-Phase-- [at] specialized-td27175880 [dot] html

On Thu, Feb 11, 2010 at 7:23 PM, Refried_ wrote:
>
> So I've got
>
> def foo1[T](a:T,b:T) = a < b
>
> But T doesn't provide < , so:
>
> def foo2[T <: Ordered[T]](a:T,b:T) = a < b
>
> but I can't call foo2(3,4) because Int doesn't provide Ordered[Int], so
>
> def foo3[T <% Ordered[T]](a:T,b:T) = a < b
>
> It finally compiles!  But I want a @specialized version for Int, so
>
> def foo4[@specialized("Int") T <% Ordered[T]](a:T,b:T) = a < b
>
> but it still boxes the Int, does a cast check, and then calls Ordered.less:
>
> public boolean foo4$mIc$sp(int, int, scala.Function1);
>  Code:
>   0:   aload_3
>   1:   iload_1
>   2:   invokestatic    #27; //Method
> scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
>   5:   invokeinterface #33,  2; //InterfaceMethod
> scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;
>   10:  checkcast       #35; //class scala/math/Ordered
>   13:  iload_2
>   14:  invokestatic    #27; //Method
> scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
>   17:  invokeinterface #39,  2; //InterfaceMethod
> scala/math/Ordered.$less:(Ljava/lang/Object;)Z
>   22:  ireturn
>
> Any suggestions?
>
> Thanks,
> Arya
> --
> View this message in context: http://old.nabble.com/%40specialized-and-%3C-tp27552066p27552066.html
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: @specialize and <
If you want a solution right now, create your own variant of Ordered (and your own implicit conversion) that is @specialized.

If you just want it to work eventually, pushing @specialized through the library is planned for later in 2.8's life (hopefully before the official release), at which point your foo4 should do what you wanted.

  --Rex

On Thu, Feb 11, 2010 at 1:22 PM, Refried_ <arya [dot] irani [at] gmail [dot] com> wrote:

Any suggestions?

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