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

array.get #22837

Open
1 of 2 tasks
khalyomede opened this issue Nov 11, 2024 · 1 comment
Open
1 of 2 tasks

array.get #22837

khalyomede opened this issue Nov 11, 2024 · 1 comment
Labels
Feature Request This issue is made to request a feature.

Comments

@khalyomede
Copy link
Contributor

khalyomede commented Nov 11, 2024

Describe the feature

Today if we access an index that do not exist on an array, the program panics:

items := [1, 2, 3]
item := items[4] // panic

V can accept a or statement, but this is not enforced by the compiler:

items := [1, 2, 3]
item := items[4] or {5} // 5

I think we should add a "safe" index lookup method, for those who want to ensure they always get an expected value.

items := [1, 2, 3]
item := items.get(4) or {5} // 5

The array.get method should return an Option (so the program is not valid unless a fallback is provided).

Use Case

Just to not forget to use a fallback, or if the program access a non existing index.

Proposed Solution

I tried to write a polyfill for []string arrays, but the attempt failed.

Other Information

It seems there is a private get method.

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

V 0.4.7 e03bd3f

Environment details (OS name and version, etc.)

V full version: V 0.4.7 e03bd3f
OS: linux, Linux version 6.8.0-48-generic (buildd@lcy02-amd64-040) (x86_64-linux-gnu-gcc-12 (Ubuntu 12.3.0-1ubuntu122.04) 12.3.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #4822.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Oct 7 11:24:13 UTC 2
Processor: 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz

getwd: /home/v
vexe: /opt/vlang/v
vexe mtime: 2024-09-26 07:47:51

vroot: OK, value: /opt/vlang
VMODULES: OK, value: /root/.vmodules
VTMP: OK, value: /tmp/v_0

env VFLAGS: "-cc gcc"

Git version: git version 2.40.3
Git vroot status: e03bd3f (370 commit(s) behind V master)
.git/config present: true

CC version: cc (Alpine 12.2.1_git20220924-r10) 12.2.1 20220924
thirdparty/tcc status: thirdparty-linuxmusl-amd64 a3e24da2

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@khalyomede khalyomede added the Feature Request This issue is made to request a feature. label Nov 11, 2024
@jorgeluismireles
Copy link

Another approach could be enforce the use of unsafe for the direct access of the elements.

items := [1, 2, 3]
item := unsafe { items[4] } // panic in your behalf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request This issue is made to request a feature.
Projects
None yet
Development

No branches or pull requests

2 participants