record-like initializer for other classes

Mateusz Romanowski romanowski.mateusz at gmail.com
Sat Jun 20 14:15:04 UTC 2020


Hi Brian,
Sorry about the attachment; put the content at the end of this message.

Understood, would be awaiting other goodies then.

Cheers,
Mateusz

On Fri, Jun 19, 2020 at 9:55 PM Brian Goetz <brian.goetz at oracle.com> wrote:

> The attachment was dropped.
>
> We are interested in generalizing some of the record goodies.  This one is
> probably one of the weaker ones, but we have thought about it. The basic
> problem is that in the general case, the parameter names and the field
> names are essentially unrelated. With records we are able to derive both
> from a common description.
>
> In general though, I think there are much higher leverage things to work
> on here.
>
> Sent from my iPad
>
> > On Jun 19, 2020, at 5:19 PM, Mateusz Romanowski <
> romanowski.mateusz at gmail.com> wrote:
> >
> > Hi,
> > Since record classes' concept has become stable, I do hope some ideas
> might
> > be migrated to less-restricted classes.
> >
> > I am, I believe, one of many programmers that need to write a lot of
> > `this.x = x;`-like assignments in constructors and therefore *used to*
> > abuse static methods' inner classes to save some characters.
> >
> > Is there any chance to marry inner class' parameter capture with
> > record-like class initializer?
> > I.e.,  for class with explicit initializer with a _FormalParameterList_:
> > * compiler generates implicit or uses compactly declared explicit
> > constructor with identical _FormalParameterList_,
> > * for any _FormalParameter_ used outside the constructor, compiler
> captures
> > it into implicit final fields *with the same name as parameter*.
> > Attached please find some translation equivalencies.
> >
> > What do you think?
> >
> > Cheers,
> > Mateusz R.
>
> ---
class B(int i) extends A {
  B {
    super((double)(i+1));
  }
}

class B extends A {
  B(int i) {
    super((double)(i+1));
  }
}

---

protected abstract class MyAction(String text, boolean visibility) extends
AbstractAction {
  MyAction {
    super(text);
  }

  @Override
  public void actionPerformed() {
    setVisible(visible);
  }
}

protected abstract class MyAction extends AbstractAction {
  protected MyAction(String text, boolean visibility) {
    super(text);
    this.visibility = visibility;
  }

  @Override
  public void actionPerformed() {
    setVisible(visible);
  }

  private final boolean visibility;
}

---

public class Counter(int initialValue) {
  private final AtomicInteger value = new AtomicInteger(initialValue);
}



public class Counter {
  public Counter(int initialValue) {
    this.value = new AtomicInteger(initialValue);
  }

  private final AtomicInteger value;
}

---

public class LocalCounter(int initialValue) {
  int value = initialValue;
}

public class LocalCounter {
  public LocalCounter(int initialValue) {
    this.value = initialValue;
  }

  int value;
}

---

class Degenerated(int a, int b) {
  void print() {
    System.out.println("value =" + a);
  }
}

class Degenerated {
  Degenerated(int a, int b) {
    this.a = a;
  }

  void print() {
    System.out.println("value =" + a);
  }

  private final int a;
}


More information about the amber-dev mailing list