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

Added toNullable method #28

Merged
merged 1 commit into from
Jan 11, 2025
Merged

Added toNullable method #28

merged 1 commit into from
Jan 11, 2025

Conversation

petrknap
Copy link
Owner

@petrknap petrknap commented Jan 9, 2025

  • This pull request does not contain any breaking change.
  • I asked the AI to briefly summarize, use and improve the feature(s), and I'm okay with the output:

Could you briefly summarize the method asNullable from the following code for me?

/**
 * @extends Optional<int>
 */
class SomeOptional extends Optional {
    /**
     * @return int|null
     */
    public function asNullable(): mixed
    {
        return $this->value;
    }
}

Absolutely! The asNullable method in your SomeOptional class returns the value of the instance, which can be either an integer or null. Essentially, it's a way to convert the optional value to a nullable type. It makes accessing the value more flexible, as it allows for handling cases where the value might not be present.

I hope this clears things up! How's your coding project going?

Could you show me how to use it?

Sure thing! Here's an example of how you might use the asNullable method in your SomeOptional class. Let's say you have an instance of SomeOptional that holds a value:

// Create an instance of SomeOptional with a value.
$optional = new SomeOptional(42);

// Use the asNullable method to get the value or null.
$value = $optional->asNullable();

if ($value !== null) {
    echo "The value is: " . $value;
} else {
    echo "The value is null.";
}

In this example, we create an instance of SomeOptional with a value of 42. Then, we call the asNullable method to retrieve the value. The code checks if the value is null and outputs either the value or a message indicating that the value is null.

I hope this helps! Let me know if you have any more questions or need further assistance with your project.

Is there any way to name the method better?

Certainly! Choosing a more descriptive name for the method can improve code readability and make its purpose clearer to other developers. Here are a few alternative names for the asNullable method:

  1. getValueOrNull
  2. toNullable
  3. optionalValue
  4. nullableValue
  5. getNullable

Each of these names emphasizes that the method retrieves a nullable value from the optional object.

Do you have a preference for one of these names, or is there a specific context that might suggest a different name? I'm happy to help you brainstorm further!

Which matches better to method ofNullable?

Given the method ofNullable, it makes sense to maintain consistency in your method naming convention. In that case, toNullable matches better with ofNullable. The consistent use of "to" and "of" can make the code more readable and intuitive:

  • ofNullable: Indicates that the method takes a nullable value.
  • toNullable: Indicates that the method converts to a nullable value.

Using similar patterns helps keep the codebase clean and easier to understand.

Does that work for you? If you need any more assistance, feel free to ask!

@petrknap petrknap self-assigned this Jan 9, 2025
@petrknap petrknap force-pushed the as-nullable-and-falsable branch from b428295 to 152bad7 Compare January 9, 2025 21:37
@petrknap petrknap changed the title Added asNullable method Added toNullable method Jan 9, 2025
@petrknap petrknap merged commit 30112a3 into main Jan 11, 2025
2 checks passed
@petrknap petrknap deleted the as-nullable-and-falsable branch January 11, 2025 08:55
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 this pull request may close these issues.

1 participant