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

ToString() implementation #1

Open
nielsvhb opened this issue May 4, 2022 · 1 comment
Open

ToString() implementation #1

nielsvhb opened this issue May 4, 2022 · 1 comment

Comments

@nielsvhb
Copy link

nielsvhb commented May 4, 2022

I love working with Option types but he thing that made us make our own implemenation is how the ToString() method works. It wraps the value in "Some(value)" or "None" in the None case. Seems a bit odd, why would you ever do it that way?

Isn't it possible to call the ToString() method on the value in the Some case and return an empty string in the None case? This makes working with the method way more useful in both Razor files/string interpolations.

Thanks

@aquacash5
Copy link

aquacash5 commented Jan 7, 2025

The to string method provided is good.

If you have an Option you can use DefaultValue .

// Returns "Hello"
Option.Some("Hello").DefaultValue("");

// Returns ""
Option<string>.None.DefaultValue("");

If you have an arbitrary T that you want to change into a string based on T or and empty string you can use Match or Map/DefaultValue.

// Returns "5"
Option.Some(5).Match(i => i.ToString(), () => "");

// Returns ""
Option<int>.None.Match(i => i.ToString(), () => "");

// Returns "5"
Option.Some(5).Map(i => i.ToString()).DefaultValue("");

// Returns ""
Option<int>.None.Map(i => i.ToString()).DefaultValue("");

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

No branches or pull requests

2 participants