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

Feature request: Add syntax sugar for dictionary access #17

Open
jaysonsantos opened this issue May 10, 2022 · 3 comments
Open

Feature request: Add syntax sugar for dictionary access #17

jaysonsantos opened this issue May 10, 2022 · 3 comments

Comments

@jaysonsantos
Copy link

Hey there, first, thank you very much for the project, it is quite handy for me.
What do you think of being able to use ? on dictionaries access?
So instead of writing this:

let data = @[
    a: 1
]
function get_value_by_key(key)
    data[key]
end

function get_value()
    get_value_by_key("b")
end

std.print(std.catch(get_value))

one could write this:

let data = @[
    a: 1
]

function get_value()
    data["b"]?
end

std.print(get_value())
@gahag
Copy link
Collaborator

gahag commented May 11, 2022

Sounds like a great feature for me! But I believe we'll have to use another symbol instead of ?, as that one is already used. Perhaps something like:

let data = @[ a: 1 ]
std.assert(data:a == 1)
std.assert(data:["a"] == 1)
std.assert(data:a:b == nil)
std.assert(data:["c"]:["d"] == nil)

Does that seem reasonable to you?

@a-moreira
Copy link
Contributor

a-moreira commented May 11, 2022

@gahag makes sense to me!
we could also go with :- to keep consistency with Bash :-)

@jaysonsantos
Copy link
Author

jaysonsantos commented May 11, 2022

@gahag sure, either way would be nice. I thought of ? because it could also short circuit functions.
I mean, the operator I said, was not to access the field itself, but to change how access work, like, first making invalid index not panicking, and then the ? to do the check of error, like rust's Result.
Or, if dicts would always return nil for invalid indexes, ? would behave like rust functions that return Optional<T>

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

No branches or pull requests

3 participants