Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Multiple Assignment with one declared, one undeclared variable #29

Closed
ruipsrosario opened this issue Mar 8, 2017 · 3 comments
Closed
Labels

Comments

@ruipsrosario
Copy link

ruipsrosario commented Mar 8, 2017

Hey there,

I was coding some stuff with Odin and I am currently facing this "problem" (not sure if it is by design or not).

So imagine you have procedures A and B which both return (AType, string) and (AnotherType, string), respectively. And you have this snippet of code:

aType, errorString := A();
anotherType, errorString := B();

The compiler will complain since the call to procedure B will attempt to redeclare errorString. However, the intent was to reuse errorString, from the declaration of calling A earlier, and declare anotherType. Due to using := the compiler will interpret as declaring both variables. Using = the compiler will complain saying that anotherType isn't defined.

Is there going to be support to declaring only some variables in a multiple return values procedure call? I know this can be fixed by just declaring all variables earlier, but since Odin does a good job at reducing LOC this could further help reducing it.

Cheers,
Rui Rosário

example code, not code from actual situation

@alichay
Copy link
Contributor

alichay commented Mar 8, 2017 via email

@ruipsrosario
Copy link
Author

ruipsrosario commented Mar 8, 2017

@zangent I don't mind forward declaring variables (I'm already used to that), I was just curious if it was something that was planned but overlooked or just something that was ignored by design.

@gingerBill
Copy link
Member

TL;DR This is by design and not a mistake.

:= Is actually : = with the type missing. So what you are doing is double declaration that variable.

// These three are equivalent in semantics
x: int; x = 123;
x: int = 123;
x := 123;

If you do have a "tuple" value on the right hand side, you either have to predeclare all the variables or remove the type (or specify the type if they are all of the same type). Tuples are not a first class type in this language and this is by design. I've found that tuples are only ever useful as return values from procedures and a named membered record is a much better thing to have (even if it is a little more verbose).

In Go, := is called a short variable declaration which will reassign variables that have previously been declared. I personally do not like this approach as it does hidden variable reassignment which is very prone to bugs (which I've had happen to me).

I hope this clears things up. You can still use := but I would either recommend declaring variables beforehand or rename that new variable.

@odin-lang odin-lang locked and limited conversation to collaborators May 12, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

4 participants