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

[Collections] Add new error-friendly get equivalent #1832

Open
drkameleon opened this issue Dec 14, 2024 · 5 comments
Open

[Collections] Add new error-friendly get equivalent #1832

drkameleon opened this issue Dec 14, 2024 · 5 comments
Assignees
Labels
→ Collections library Issues related to the standard library new feature New feature/addition

Comments

@drkameleon
Copy link
Collaborator

The main idea:

  • it will work like get with blocks, dictionaries, etc
  • if the requested index is not there, instead of throwing an error, it will return null
  • suggested name: fetch
  • usage case: x: (fetch d 'field) ?? "default"
@drkameleon drkameleon added library Issues related to the standard library new feature New feature/addition → Collections labels Dec 14, 2024
@drkameleon drkameleon self-assigned this Dec 14, 2024
@Eloitor
Copy link
Contributor

Eloitor commented Dec 14, 2024

why not get.default: null ?

@Eloitor
Copy link
Contributor

Eloitor commented Dec 14, 2024

x: get.default: "default" d 'field

@drkameleon
Copy link
Collaborator Author

why not get.default: null ?

Good point!

If it was a normal stdlib function, that could work.
The problem is get (and set) are used extensively and mostly auto-transformed at AST level (.field which we have recently added could be an exception, since it's for objects only).

But having to check if the attribute is there for normal array/dictionary access, apart from slow (performance-wise), it would be close to impossible... 😉

@Eloitor
Copy link
Contributor

Eloitor commented Dec 14, 2024

why not have get.default: in the stdlib, and transform it into a (hidden?) vm operation fetch when "compiling"?

@drkameleon
Copy link
Collaborator Author

drkameleon commented Dec 14, 2024

why not have get.default: in the stdlib, and transform it into a (hidden?) vm operation fetch when "compiling"?

AST transformation generally plays with the main tree structure when doing its magic. But attributes are not included.
If they were, it would defeat the very cascade-style nature of attributes in Arturo...

I mean...

do.default [
    get ... ; is this a `get.default`? Could be, or could be not... we cannot know on the AST level
]

Technicalities aside, something that I actually wanted (probably for months lol) is a quick way to short-circuit a get, and that: x: (fetch d 'field) ?? "default" looks short enough...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
→ Collections library Issues related to the standard library new feature New feature/addition
Projects
None yet
Development

No branches or pull requests

2 participants