Skip to content

Compound Types using @specialized type parameter fail with stackoverflow #10689

Open
@wsargent

Description

@wsargent

I'm following the example with AOP style mixin composition stacks using compound types, and it looks like if the apply() method is used then it fails with stack overflow. It works with everything else.

So following the link http://jonasboner.com/aop-style-mixin-composition-stacks-in-scala/ everything works fine

// WORKS!

trait Stuff {
  def doStuff(): Unit
}

class RealStuff extends Stuff {
  def doStuff(): Unit = println("doing real stuff")
}

trait LoggableStuff extends Stuff {
  abstract override def doStuff(): Unit = {
    println("logging enter")
    super.doStuff
    println("logging exit")
  }
}

val stuff = new RealStuff with LoggableStuff
stuff.doStuff()

However if I change the base trait to Function0[Unit] it fails with stack overflow.

class RealFunction extends Function0[Unit] {
  def apply(): Unit = println("doing real stuff")
}

trait LoggableFunction extends Function0[Unit] {
  abstract override def apply(): Unit = {
    println("logging enter")
    super.apply()
    println("logging exit")
  }
}

val fn = new RealFunction with LoggableFunction
fn.apply()

But copying Function0 and taking out the @specialized annotation makes it work:

// trait Function0[@specialized(Specializable.Primitives) +R] extends AnyRef { self =>
trait DerpFunction0[+R] extends AnyRef { self =>
  /** Apply the body of this function to the arguments.
   *  @return   the result of function application.
   */
  def apply(): R

  override def toString() = "<function0>"
}

class RealFunction extends DerpFunction0[Unit] {
  def apply(): Unit = println("doing real stuff")
}

trait LoggableFunction extends DerpFunction0[Unit] {
  abstract override def apply(): Unit = {
    println("logging enter")
    super.apply()
    println("logging exit")
  }
}

val fn = new RealFunction with LoggableFunction
fn.apply()

I think this is a bug, as there's nothing in documentation saying compound types work unless you have a type parameter with a @specialized annotation.

Activity

changed the title [-]Compound Types fail mysteriously with @specialized(Specializable.Primitives) [/-] [+]Compound Types using @specialized(Specializable.Primitives) type parameter fail with stackoverflow[/+] on Jan 13, 2018
changed the title [-]Compound Types using @specialized(Specializable.Primitives) type parameter fail with stackoverflow[/-] [+]Compound Types using @specialized type parameter fail with stackoverflow[/+] on Jan 13, 2018
added this to the Backlog milestone on Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @wsargent@SethTisue@hrhino

        Issue actions

          Compound Types using @specialized type parameter fail with stackoverflow · Issue #10689 · scala/bug