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

Abstract structural (?) type question

2 replies
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.

hi,

considering this:

trait SkipQuadTreeImpl[ A ] {
protected type LeafType <: { def value: A }

def +=( elem: A ) : this.type = {
insertLeaf( elem )
this
}

def add( elem: A ) : Boolean = {
val oldLeaf = insertLeaf( elem )
if( oldLeaf == null ) true else oldLeaf.value != elem
}

protected def insertLeaf( elem: A ) : LeafType
}

am i running into danger that this definition of `LeafType` will general structural types / runtime reflection stuff, or any other performance penalties? or should i rather do this:

object SkipQuadTreeImpl {
trait Leaf[ A ] { def value: A }
}
trait SkipQuadTreeImpl[ A, L <: SkipQuadTreeImpl.Leaf[ A ]] {
def +=( elem: A ) : this.type = {
insertLeaf( elem )
this
}

def add( elem: A ) : Boolean = {
val oldLeaf = insertLeaf( elem )
if( oldLeaf == null ) true else oldLeaf.value != elem
}

protected def insertLeaf( elem: A ) : L
}

or this is virtually the same?

thanks, -sciss-

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Abstract structural (?) type question


On Fri, Oct 21, 2011 at 12:41 AM, Sciss <contact [at] sciss [dot] de> wrote:
hi,

considering this:

trait SkipQuadTreeImpl[ A ] {
  protected type LeafType <: { def value: A }

  def +=( elem: A ) : this.type = {
     insertLeaf( elem )
     this
  }

  def add( elem: A ) : Boolean = {
     val oldLeaf = insertLeaf( elem )
     if( oldLeaf == null ) true else oldLeaf.value != elem
  }

  protected def insertLeaf( elem: A ) : LeafType
}

am i running into danger that this definition of `LeafType` will general structural types / runtime reflection stuff, or any other performance penalties? or should i rather do this:

object SkipQuadTreeImpl {
  trait Leaf[ A ] { def value: A }
}
trait SkipQuadTreeImpl[ A, L <: SkipQuadTreeImpl.Leaf[ A  ]] {
  def +=( elem: A ) : this.type = {
     insertLeaf( elem )
     this
  }

  def add( elem: A ) : Boolean = {
     val oldLeaf = insertLeaf( elem )
     if( oldLeaf == null ) true else oldLeaf.value != elem
  }

  protected def insertLeaf( elem: A ) : L
}

or this is virtually the same?

What does javap tell you?

Cheers,

 


thanks, -sciss-




--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Abstract structural (?) type question

i don't know....

trait SkipQuadTreeImpl[ A ] {
protected type LeafType <: { def value: A }
def gaga : A
protected def insertLeaf( elem: A ) : LeafType
insertLeaf( gaga ).value
}

:javap -v SkipQuadTreeImpl
Compiled from ""
public interface SkipQuadTreeImpl extends scala.ScalaObject
SourceFile: ""
Scala: length = 0x

Signature: length = 0x2
00 0B
InnerClass:
public abstract #17= #14 of #16; //SkipQuadTreeImpl=class SkipQuadTreeImpl of class
minor version: 0
major version: 49
Constant pool:
const #1 = Asciz SourceFile;
const #2 = Asciz ;
const #3 = Asciz gaga;
const #4 = Asciz ()Ljava/lang/Object;;
const #5 = Asciz ()TA;;
const #6 = Asciz Signature;
const #7 = Asciz insertLeaf;
const #8 = Asciz (Ljava/lang/Object;)Ljava/lang/Object;;
const #9 = Asciz (TA;)Ljava/lang/Object;;
const #10 = Asciz Scala;
const #11 = Asciz Ljava/lang/Object;Lscala/ScalaObject;;
const #12 = Asciz InnerClasses;
const #13 = Asciz SkipQuadTreeImpl;
const #14 = class #13; // SkipQuadTreeImpl
const #15 = Asciz ;
const #16 = class #15; //
const #17 = Asciz SkipQuadTreeImpl;
const #18 = Asciz java/lang/Object;
const #19 = class #18; // java/lang/Object
const #20 = Asciz scala/ScalaObject;
const #21 = class #20; // scala/ScalaObject

{
public abstract java.lang.Object gaga();
Signature: length = 0x2
00 05

public abstract java.lang.Object insertLeaf(java.lang.Object);
Signature: length = 0x2
00 09

}

i can't spot the call to `value` there....

On 20 Oct 2011, at 23:51, √iktor Ҡlang wrote:

>
>
> On Fri, Oct 21, 2011 at 12:41 AM, Sciss wrote:
> hi,
>
> considering this:
>
> trait SkipQuadTreeImpl[ A ] {
> protected type LeafType <: { def value: A }
>
> def +=( elem: A ) : this.type = {
> insertLeaf( elem )
> this
> }
>
> def add( elem: A ) : Boolean = {
> val oldLeaf = insertLeaf( elem )
> if( oldLeaf == null ) true else oldLeaf.value != elem
> }
>
> protected def insertLeaf( elem: A ) : LeafType
> }
>
> am i running into danger that this definition of `LeafType` will general structural types / runtime reflection stuff, or any other performance penalties? or should i rather do this:
>
> object SkipQuadTreeImpl {
> trait Leaf[ A ] { def value: A }
> }
> trait SkipQuadTreeImpl[ A, L <: SkipQuadTreeImpl.Leaf[ A ]] {
> def +=( elem: A ) : this.type = {
> insertLeaf( elem )
> this
> }
>
> def add( elem: A ) : Boolean = {
> val oldLeaf = insertLeaf( elem )
> if( oldLeaf == null ) true else oldLeaf.value != elem
> }
>
> protected def insertLeaf( elem: A ) : L
> }
>
> or this is virtually the same?
>
> What does javap tell you?
>
> Cheers,
> √
>
>
>
> thanks, -sciss-
>
>
>
>

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