-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
Use underscore (_
) as a "default" key for Dictionary
#2736
Comments
I don't think this is a common use-case for dictionaries and you can always make a method that would know to fallback on a special key or a predefined value. func get_webxr_feature(key : String):
if (WEBXR_FEATURE_MAPPING.has(key)):
return WEBXR_FEATURE_MAPPING[key]
return WEBXR_FEATURE_MAPPING["_"] # or just ClientMode.CLIENT_LOCAL |
See also #1321. |
I don't think there is much more overhead than with the |
Also, what happens if you do want to put a _ as regular key into a dictionary? I don't think having such kludges is a good idea. They are likely to trip people up sooner or later. Sounds like a violation of the Principle of least astonishment. If we really want to have this feature, it might be better to consider something like Python's defaultdict. But I am not convinced that it is worth it the additional type. As @pycbouh has pointed out, the issue can be worked around easily. |
_
) as a "default" key for Dictionary
My original reasoning was that since the |
Describe the project you are working on
I am working on a VR application with low-latency streaming
Describe the problem or limitation you are having in your project
I am mapping WebXR strings to enum values using a dictionary, like so:
But I'd like to avoid the overhead of using the
.get()
function every time to handle null coalescence.Describe the feature / enhancement and how it helps to overcome the problem or limitation
I noticed that the
match...case
branch uses the_
token as a wildcard. I believe that it would be intuitive for GDScript dictionaries to have this property as well. Instead of defining a default value during the.get()
function call, I think it would be easier for programmers to define a default key-value pair upon declaration.Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Accessing a dictionary this way is redundant and unintuitive:
But with the underscore wildcard, default values are be a breeze:
If this enhancement will not be used often, can it be worked around with a few lines of script?
While it's possible to have default keys declared in the function call, it is easier to have them declared upon creation.
Is there a reason why this should be core and not an add-on in the asset library?
This is a change to a GDScript core object, so there's no way to implement this otherwise.
The text was updated successfully, but these errors were encountered: