Skip to content

The dot-shorthands feature is not working in some operator expressions. #60671

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

Open
mmcdon20 opened this issue May 4, 2025 · 6 comments
Open

Comments

@mmcdon20
Copy link

mmcdon20 commented May 4, 2025

It fails when using the shorthand in the left-hand side of a binary operator:

int a = 1 + .parse('1'); // works
int b = .parse('1') + 1; // Error: No type was provided to find the dot shorthand 'parse'.

It fails when using some unary operators:

bool a = !.parse('true'); // works
int b = -.parse('1'); // Error: No type was provided to find the dot shorthand 'parse'.
int c = ~.parse('1'); // Error: No type was provided to find the dot shorthand 'parse'.
@FMorschel
Copy link
Contributor

FYI @kallentu

@mraleph
Copy link
Member

mraleph commented May 4, 2025

I think it is working as intended. In an expression like a + 1, -a and ~a a can be anything, so you don't have any information to resolve .parse. In the expressions like !a and 1 + a a is actually constrained.

@mmcdon20
Copy link
Author

mmcdon20 commented May 4, 2025

@mraleph the type of the variable is used as lookup context when doing a dot-shorthand.

The following works for example:

int a = .parse('-1').abs();

But it obviously would not work for:

var a = .parse('-1').abs();

@mraleph
Copy link
Member

mraleph commented May 4, 2025

The spec mentions these cases specifically. Search NOT ALLOWED, ALL .id ARE ERRORS! in the specification.

https://github.com/dart-lang/language/blob/main/working/3616%20-%20enum%20value%20shorthand/proposal-simple-lrhn.md

@mmcdon20
Copy link
Author

mmcdon20 commented May 4, 2025

Alright it does mention this case as an error.

I guess the question then is how come operators can't get a context type for the left hand side?

@RohitSaily
Copy link
Contributor

Had to think about this for a while because it seemed nonintuitive, but the decision seems to make sense. Consider we do something odd

class MyInt
{	final int mine;
	MyInt(this.mine);
	int operator +(int i)
	{	return mine+i;
	}
	// ...
}

Suppose MyInt also has parse. Then we can't be sure what is being referenced in the dot-shorthand in int b = .parse('1') + 1;. It could be that of int or MyInt

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

No branches or pull requests

4 participants