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

Use underscore (_) as a "default" key for Dictionary #2736

Closed
shraiwi opened this issue May 17, 2021 · 5 comments
Closed

Use underscore (_) as a "default" key for Dictionary #2736

shraiwi opened this issue May 17, 2021 · 5 comments

Comments

@shraiwi
Copy link

shraiwi commented May 17, 2021

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:

# ClientMode is an enum
const WEBXR_FEATURE_MAPPING : Dictionary = {
	"local-floor": ClientMode.CLIENT_VR_3DOF,
	"bounded-floor": ClientMode.CLIENT_VR_6DOF,
};

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:

const WEBXR_FEATURE_MAPPING : Dictionary = {
	"local-floor": ClientMode.CLIENT_VR_3DOF,
	"bounded-floor": ClientMode.CLIENT_VR_6DOF,
};

# for multiple calls, this gets unwieldy
WEBXR_FEATURE_MAPPING.get("local", ClientMode.CLIENT_FLAT); # returns ClientMode.CLIENT_FLAT
WEBXR_FEATURE_MAPPING.get("local-floor", ClientMode.CLIENT_FLAT); # returns ClientMode.CLIENT_VR_3DOF

But with the underscore wildcard, default values are be a breeze:

const WEBXR_FEATURE_MAPPING : Dictionary = {
	"local-floor": ClientMode.CLIENT_VR_3DOF,
	"bounded-floor": ClientMode.CLIENT_VR_6DOF,
	_: ClientMode.CLIENT_FLAT, # '_' denotes a default value
};

# this stays clear and concise
WEBXR_FEATURE_MAPPING["local"]; # returns ClientMode.CLIENT_FLAT
WEBXR_FEATURE_MAPPING["local-floor"]; # returns ClientMode.CLIENT_VR_3DOF

# of course, we can always override the default in the function call
WEBXR_FEATURE_MAPPING.get("local", ClientMode.CLIENT_LOCAL); # returns ClientMode.CLIENT_LOCAL

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.

@YuriSizov
Copy link
Contributor

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

@Calinou
Copy link
Member

Calinou commented May 17, 2021

See also #1321.

@KoBeWi
Copy link
Member

KoBeWi commented May 17, 2021

But I'd like to avoid the overhead of using the .get() function every time to handle null coalescence.

I don't think there is much more overhead than with the [] operator or with the proposed changes.

@zinnschlag
Copy link

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.

@Calinou Calinou changed the title Underscore as a dictionary default key Use underscore (_) as a "default" key for Dictionary May 18, 2021
@shraiwi
Copy link
Author

shraiwi commented May 27, 2021

Sounds like a violation of the Principle of least astonishment.

My original reasoning was that since the match..case pattern uses the underscore as a wildcard, the dictionary should too, but your reasoning makes sense as well. The group that it benefits from a change like this is so small that it really isn't worth implementing it. Thanks for the feedback!

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

No branches or pull requests

5 participants