Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

concrete classes implementing structural types are pessimized #3394

Closed
scabug opened this issue May 5, 2010 · 2 comments
Closed

concrete classes implementing structural types are pessimized #3394

scabug opened this issue May 5, 2010 · 2 comments

Comments

@scabug
Copy link

scabug commented May 5, 2010

In the course of exercising abstract hierarchies I frequently find myself wanting to specify a type in terms of the methods it must implement without wanting to impose the additional requirement of implementing a specific interface. One can do this but the penalty for doing so is severe and, it appears to me, unnecessary.

Consider:

trait Trait {
  type Foo <: { def bar(x: Int): Int }
  
  def foobar(foo: Foo) = foo.bar(10)
}

class Concrete1 extends Trait {
  class Foo() { def bar(x: Int) = 1 }
}

class Concrete2 extends Trait {
  class Foo() { def bar(x: Int) = 1 }
  
  override def foobar(foo: Foo) = foo.bar(10)
}

In Concrete2 we override the method with a syntactically identical version of the inherited method. The impact is profound.

In Concrete1: a call to foobar forwards the target object and argument to Trait.foobar, where the integer is boxed and placed in an array, the method handle is looked up, the reflective call is issued, the result is unboxed, and it is returned to the calling class.

In Concrete2: a call to foobar results in a direct unboxed call to foo.bar.

But surely if manually overriding each method with an exact copy achieves this, the compiler could assume the responsibility.

@scabug
Copy link
Author

scabug commented May 5, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3394?orig=1
Reporter: @paulp

@scabug
Copy link
Author

scabug commented May 13, 2010

@paulp said:
Noting for the future: I brought this up on scala-debate and received replies from martin and iulian among others.

http://scala-programming-language.1934581.n4.nabble.com/implementing-structurally-defined-types-td2131437.html

In particular martin says: "In principle this is possible, and it could indeed be very useful to do this. In practice it's harder than it looks (aren't things always?)
What about fields that are private in the base trait? How to estimate when such copying is beneficial and not just a waste of code? How to do this in the presence of separate compilation?"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant