Git Product home page Git Product logo

Comments (2)

dwijnand avatar dwijnand commented on June 3, 2024

Given

class A; object A1 extends A
class B; object B1 extends B; object B2 extends B
class C; object C1 extends C

object T {
  def t = {
    val x = new Foo(A1, B1)
    x.copy(b = B2)
  }
}

Version 1

Foo defined as:

final class Foo(a: A, b: B) {
  def copy(a: A = a , b: B = b) = new Foo(a, b)
}

Then the final bytecode contains these:

<synthetic> def copy$default$1(): A = Foo.this.a;
<synthetic> def copy$default$2(): B = Foo.this.b;

def t(): Foo = {
  val x: Foo = new Foo(A1, B1);
  {
    <artifact> val x$1: B2.type = B2;
    <artifact> val x$2: A = x.copy$default$1();
    x.copy(x$2, x$1)
  }
};

Version 2

Add a parameter at the front and overload:

final class Foo(c: C, a: A, b: B) {
  def this(a: A, b: B) = this(C1, a, b)
  def copy(a: A, b: B) = new Foo(c, a, b)
  def copy(c: C = c, a: A = a, b: B = b) = new Foo(c, a, b)
}

Then you get:

<synthetic> def copy$default$1(): C = Foo.this.c;
<synthetic> def copy$default$2(): A = Foo.this.a;
<synthetic> def copy$default$3(): B = Foo.this.b;

def t(): Foo = {
  val x: Foo = new Foo(A1, B1);
  {
    <artifact> val x$1: B2.type = B2;
    <artifact> val x$2: C = x.copy$default$1();
    <artifact> val x$3: A = x.copy$default$2();
    x.copy(x$2, x$3, x$1)
  }
};

Note

copy$default$1 and copy$default$2 have new, completely incompatible types.

This would:

  • fail MiMa
  • fail with a ClassCastException at runtime (or some other fatal error), and
  • requires a recompilation to fix.

from contraband.

dwijnand avatar dwijnand commented on June 3, 2024

I checked, and my assumption was wrong: MiMa doesn't fail..

Opened lightbend/mima#136

from contraband.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.