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

Add built-in is_nullable() function for None/Undefined checks #1600

Closed
suin opened this issue Aug 26, 2024 · 1 comment · Fixed by #1606
Closed

Add built-in is_nullable() function for None/Undefined checks #1600

suin opened this issue Aug 26, 2024 · 1 comment · Fixed by #1606

Comments

@suin
Copy link

suin commented Aug 26, 2024

Feature Request

Is your feature request related to a problem? Please describe:
When working with KCL, I often need to check if a value is "nullish" (either None or Undefined). Currently, this requires using constructs like value in [None, Undefined] or value is None or value is Undefined. While functional, these approaches can be verbose and may lead to inconsistencies across different codebases.

Describe the feature you'd like:
I propose adding a new built-in function is_nullable(value) to KCL. This function would return True if the value is None or Undefined, and False otherwise. For example:

if is_nullable(some_value):
    print("Value is None or Undefined")
else:
    print("Value is not None and not Undefined")

This would provide a concise, readable, and standardized way to perform this common check.

Describe alternatives you've considered:

  1. Continue using value in [None, Undefined]
  2. Use value is None or value is Undefined

While these alternatives work, they lack the clarity and potential for optimization that a dedicated built-in function could provide.

Teachability, Documentation, Adoption, Migration Strategy:
The is_nullable function would be straightforward to teach and document. It follows a naming convention similar to other programming languages and its purpose is self-explanatory.

Adoption scenarios:

  1. Simplifying null checks in conditional statements:

    if not is_nullable(config.timeout):
        # Use the timeout value
  2. Cleaning up default value assignments:

    effective_value = alt_value if is_nullable(primary_value) else primary_value
  3. Validating function inputs:

    assert not is_nullable(username), "Username must be provided"

Migration would be straightforward as the function doesn't break existing code. Developers could gradually adopt it in new code or during refactoring.

@suin
Copy link
Author

suin commented Aug 26, 2024

'is_nullish' might actually be a better name than 'is_nullable'. It more accurately describes the function's purpose of checking for both None and Undefined.

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.

1 participant