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

Declare many variables which have the same type just in one line? #536

Closed
littlewhitecloud opened this issue Dec 22, 2024 · 5 comments · Fixed by #540
Closed

Declare many variables which have the same type just in one line? #536

littlewhitecloud opened this issue Dec 22, 2024 · 5 comments · Fixed by #540

Comments

@littlewhitecloud
Copy link
Contributor

I have noticed that when declaring variables in jou, we need to use a lot of lines which is unnecessary.

ax:int
ay:int
bx:int
by:int
...

So should we create a syntax to combine them into just one line?

@Akuli
Copy link
Owner

Akuli commented Dec 24, 2024

I think we could add syntax like x, y, z: int. It would be similar to int x, y, z; in C. This syntax would be useful/convenient, but there is a problem with it.

Usefulness: At first I thought we don't need this because Python doesn't have it, but Jou needs this more than Python. For example, consider the following code, taken from last year's AoC solutions:

x: long
y: long
z: long
dx: long
dy: long
dz: long
assert fscanf(f, "%lld, %lld, %lld @ %lld, %lld, %lld\n", &x, &y, &z, &dx, &dy, &dz) == 6
result[i] = MovingPoint{start = [x,y,z], speed = [dx,dy,dz]}

In Python, I would probably do this instead:

x, y, z, dx, dy, dz = map(int, re.findall("-?\d+", line))
result.append(MovingPoint(start=(x, y, z), speed=(dx, dy, dz))

Problem: What should the following do? Is x set to 123 or is it uninitialized/undefined?

x, y: int = 123

If x is set to 123, then without the type annotation this would be x, y = 123. This looks like unpacking in Python, so it's just confusing and bad.

If x is uninitialized/undefined, this will confuse people who don't know it. They will write x,y,z: int = 0 and think that all variables are zero, and now their program contains UB and they wonder why it behaves weirdly. (This happens in C.)

@Akuli
Copy link
Owner

Akuli commented Dec 24, 2024

It's tempting to add [x, y, z]: int[3], but I think it's not as simple as Jou code should be.

@Akuli
Copy link
Owner

Akuli commented Dec 28, 2024

I think:

  • x, y: int should be supported
  • x, y = 0 should fail with error cannot assign to multiple comma-separated variables
  • x, y: int = 0 should fail with error cannot assign to multiple comma-separated variables

@littlewhitecloud What do you think?

@littlewhitecloud
Copy link
Contributor Author

littlewhitecloud commented Dec 29, 2024

I think:

  • x, y: int should be supported
  • x, y = 0 should fail with error cannot assign to multiple comma-separated variables
  • x, y: int = 0 should fail with error cannot assign to multiple comma-separated variables

@littlewhitecloud What do you think?

I agree.

Is there a syntax for assign multiple variables like this?

x = y = 0

If so, I think we should raise error like this:

x, y: int = 0 -> cannot assign to multiple comma-separated variables with a single value, try to use syntax like this: x = y = 0

@Akuli
Copy link
Owner

Akuli commented Dec 30, 2024

Is there a syntax for assign multiple variables like this?

x = y = 0

I thought so, but turns out there isn't. It can be added later if needed.

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 a pull request may close this issue.

2 participants