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

Create an alias for findObject, like getOrPut #110

Closed
sschuberth opened this issue Jan 23, 2020 · 4 comments · Fixed by #112
Closed

Create an alias for findObject, like getOrPut #110

sschuberth opened this issue Jan 23, 2020 · 4 comments · Fixed by #112

Comments

@sschuberth
Copy link
Contributor

Personally, I have to say that I find the name findObject for the variant that is "setting context.obj if one is not found" very counter-intuitive. How about renaming / creating an alias function for it named getOrPut, resembling Kotlin's function for maps?

@ajalt
Copy link
Owner

ajalt commented Jan 23, 2020

I can see your point about making the naming more intuitive. There are five related functions, and I don't think it makes sense to rename just one. I'm open to renaming all of them (and deprecating the current names) if we can come up with consistent names for all five.

The five functions are:

  1. Context.findObject() which returns a nullable value.
  2. Context.findObject(default) which sets the default value if not found.
  3. CliktCommand.findObject() extension that returns a ReadOnlyProperty that calls the nullable version of Context.findObject.
  4. CliktCommand.findObject(default) extension that returns a ReadOnlyProperty that calls the default value version of Context.findObject.
  5. CliktCommand.requireObject() extension that returns a ReadOnlyProperty that calls the nullable version of Context.findObject but throws an NPE if thee value isn't found.

Here's one set of possibilities:

  1. Context.getObjectOrNull()
  2. Context.getOrPutObject()
  3. CliktCommand.contextObjectOrNull()
  4. CliktCommand.contextGetOrPutObject()
  5. CliktCommand.contextObject()

Those names are closer to what the stdlib uses for similar concepts, but I'm not a fan of how verbose they are. They also don't suggest the fact that these function search up the ancestors the way "find" does. I'm open to better suggestions.

@sschuberth
Copy link
Contributor Author

Thanks for being open to renaming these! How about:

  1. Context.findObject() (keep as-is)
  2. Context.findOrSetObject()
  3. CliktCommand.findObject() (keep as-is)
  4. CliktCommand.findOrSetObject()
  5. CliktCommand.requireObject() (keep as-is)

@ajalt
Copy link
Owner

ajalt commented Jan 23, 2020

Yeah, I like those names.

@sschuberth
Copy link
Contributor Author

Thank you for the quick implementation!

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

Successfully merging a pull request may close this issue.

2 participants