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

Deprecate the var keyword #3301

Closed
axic opened this issue Dec 11, 2017 · 16 comments
Closed

Deprecate the var keyword #3301

axic opened this issue Dec 11, 2017 · 16 comments

Comments

@axic
Copy link
Member

axic commented Dec 11, 2017

I do not think there is any proper legitimate use left for the var keyword therefore I suggest we remove it.

In for loops already a type is suggested, instead of tuple declarations tuple assignments can be used and there is no need for the var for functions as function types can be used now.

Is there any other use case for var?

In #2558 I proposed to replace it with auto, but this issue would override that and just drop it.

@axic
Copy link
Member Author

axic commented Dec 22, 2017

@chriseth any arguments for keeping var?

@chriseth
Copy link
Contributor

No, I am not a big fan of auto in C++ either.

@axic
Copy link
Member Author

axic commented Dec 22, 2017

The task here is to extend libsolidity/analysis/SyntaxChecker with a visitor for VariableDeclaration where the typeName is empty (means "var").

The compiler should emit a warning for such instances, unless the experimental mode V050 is turned on, in which case it should be an error.

@briandsutton
Copy link

Hi, I'm trying to understand something here. I understand, the use of 'var' keyword was deprecated in solidity version 0.4.20 (https://github.com/ethereum/solidity/releases/tag/v0.4.20), however the documentation for tuple assignments for that version shows the use of the 'var' keyword (https://solidity.readthedocs.io/en/v0.4.20/control-structures.html#assignment).

Is this use still correct? In case it's not, how should it be used?

@axic
Copy link
Member Author

axic commented Mar 6, 2018

@briandsutton thanks for noticing, it was left by mistake. Tuple assignments will be changed as proposed in #3314.

@jtakalai
Copy link

jtakalai commented Mar 13, 2018

So... what's the resolution on how tuple assignments are done in the future? Instead of

var (,a,,,,b,,,,,c,,,,,) = crazyfunction()

we'd have to first introduce the types like so:

uint a;
string b;
bytes32 c;
(,a,,,,b,,,,,c,,,,,) = crazyfunction()

I don't see how that is an improvement. Even the following would be neat enough:

(,uint a,,,,string b,,,,,bytes32 c,,,,,) = crazyfunction()

@OTTTO
Copy link

OTTTO commented Mar 23, 2018

potentially var could only be used for tuple destructuring.

@ellis2323
Copy link

ellis2323 commented Mar 26, 2018

So what's the fix for the 0.4.21. I have many functions like:
var (,a,,,,b,,,,,c,,,,,) = crazyfunction()
auto doesn't work. And no fix in the documentation.

@jtakalai
Copy link

jtakalai commented Mar 26, 2018

@OTTTO right, I'm not protesting deprecating var per se, just its one and only very nice use case probably should be re-thought somehow...

@JohnAllen
Copy link
Contributor

@ellis2323 #3301 (comment)

@dukei
Copy link

dukei commented Mar 31, 2018

How about var automatically preserves storage of the variable?
When using var the type is automatically inferred so you should not specify it explicitly.

For example there is struct OuterType.InnerType and function foo() returns (OuterType.InnerType storage)

With var I can write:

var x = foo();

Without var I should write:

OuterType.InnerType storage x = foo();

The latter case is very verbose. I would prefer var in this case.

What is about this usecase?

@chriseth
Copy link
Contributor

chriseth commented Apr 3, 2018

@dukei we want to introduce type aliases for this case. For now, please bear with verbosity - I'm a big fan of the Zen of Python: https://www.python.org/dev/peps/pep-0020/#id3

@jtakalai
Copy link

jtakalai commented Apr 24, 2018

@chriseth except this isn't Python. Python doesn't require explicit type declarations, so Python's zen wouldn't be broken by disallowing automatic type inference since there is none to begin with -___-

The whole point of static type checking is that the pieces won't fit wrong, even if you don't explicitly spell out the types. Also, have you considered this form of tuple de-construction?

(,uint a,,,,string b,,,,,bytes32 c,,,,,) = crazyfunction()

jtakalai added a commit to streamr-dev/marketplace-contracts that referenced this issue Apr 24, 2018
Because it's still so much cleaner. Will be reverted when var *really* is deprecated. ethereum/solidity#3301
@chriseth
Copy link
Contributor

@jtakalai yes, this is exactly what we will be implementing for the next release.

jtakalai added a commit to streamr-dev/marketplace-contracts that referenced this issue Sep 4, 2018
@chriseth notified in ethereum/solidity#3301 that tuple declarations are implemented, removed var, and other warnings too.
@ghost
Copy link

ghost commented Jun 30, 2019

OuterType.InnerType storage x = foo();
var x = foo();

I want the ability to change functions. I don't believe functions are a type. It is a grave mistake to depreciate var. Correct me if I'm wrong.

@chriseth
Copy link
Contributor

chriseth commented Jul 1, 2019

There are function types.

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

No branches or pull requests

8 participants