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

Add support to GetIntOrZero() #3372

Open
lock9 opened this issue Mar 21, 2024 · 1 comment
Open

Add support to GetIntOrZero() #3372

lock9 opened this issue Mar 21, 2024 · 1 comment
Labels
compiler Go smart contract compiler enhancement Improving existing functionality I2 Regular impact S4 Routine U4 Nothing urgent
Milestone

Comments

@lock9
Copy link

lock9 commented Mar 21, 2024

Is your feature request related to a problem? Please describe.

The following code will throw an 'Invalid Cast Exception' in runtime:

	balance := storage.Get(ctx, from).(int)
	balance += amount
	storage.Put(ctx, from, balance)

This is because it returns nil when the key is not present.

Describe the solution you'd like

I like how Neow3j solves this problem by exposing a GetIntOrZero method. As the name suggests, it will return 0 if the key is not present.

Reference:
https://github.com/neow3j/neow3j/blob/f2b243a5a01f864a3ee9b98de52458ad534a2f88/devpack/src/main/java/io/neow3j/devpack/Storage.java#L165

Describe alternatives you've considered

I'm checking for nil before using it.

Additional context

Casting will no longer be necessary with this improvement. Another change that could help, would be to allow Get calls without the context parameter.

Don't forget to add labels!

  • component label: compiler
  • category: enhancement
@lock9 lock9 added feature Completely new functionality I2 Regular impact labels Mar 21, 2024
@roman-khimov roman-khimov added U4 Nothing urgent enhancement Improving existing functionality S4 Routine compiler Go smart contract compiler and removed feature Completely new functionality labels Mar 22, 2024
@AnnaShaleva AnnaShaleva added this to the v0.107.0 milestone Mar 25, 2024
@roman-khimov
Copy link
Member

Related to #3381, likely can even be replaced by it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Go smart contract compiler enhancement Improving existing functionality I2 Regular impact S4 Routine U4 Nothing urgent
Projects
None yet
Development

No branches or pull requests

3 participants