Skip to content

Conversation

sgrekhov
Copy link
Contributor

In case of final variables there is an error that the variable is already initialized and this error may shadow the tested one. But I don't see another way how to test the initializer list behavior.

Copy link
Member

@eernstg eernstg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, with a handful of comments.


class C1 extends A {
int x;
this: x = 1, super(-1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an error to use this: here unless there is a primary constructor. To make it an in-body declaring constructor it needs to have a formal parameter list, e.g., ().

enum E1 {
e0();
final int x;
this : x = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as line 28. Also, const must be specified explicitly because this is an in-body constructor (so it's treated similarly to other in-body constructors).

enum E4 {
e0();
final int x;
this() : x = 4;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

enum E5 {
e0(5);
final int x;
this(int x) : x = x;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const


class C1 extends A {
int x;
this: super(-1), x = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this().

/// `k2` has an initializer list with the same elements in the same order.
///
/// @description Check that it is a compile-time error if a parameter of a
/// declaring constructor is initialized both in the constructor and in the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand 'in the constructor'. We can't initialize the same instance variable using more than one of: an initializing formal, an initializer list element, an initializing expression in the instance variable declaration, or a declaring formal parameter. The latter can use names from the formal parameter list of a primary constructor, if any, but we're more interested in the multiple-initializations error, so it's fine to just have an initializing expression that doesn't use the special scoping of primary constructors (int x = 1; will do just fine, both when there is a primary constructor and when there isn't any).

}

class C4(super.z) extends A {
this: z = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is different because z is declared by A and hence C4 does not initialize z, that's done by the A constructor which is executed as an implicit super(z).

So the error caused by z = 0 is that there is no such instance variable, not that we're initializing anything twice.

I'm trying to come up with a way to include this, but I suspect that C4 should just be omitted because it isn't testing anything about multiple initializations of instance variables.

}

class C14 extends A {
this(super.z) : z = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be omitted, similarly to C4.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants