Skip to content

feat: add Try-prefixed methods#17

Draft
duydang2311 wants to merge 5 commits into
sblom:mainfrom
duydang2311:main
Draft

feat: add Try-prefixed methods#17
duydang2311 wants to merge 5 commits into
sblom:mainfrom
duydang2311:main

Conversation

@duydang2311

@duydang2311 duydang2311 commented Jul 28, 2022

Copy link
Copy Markdown

This PR adds Try-prefixed methods, including: TryConstruct, TryExecute, TryExtract.

@oparkerj

Copy link
Copy Markdown
Contributor

This should also include the extension variant of the methods, which is where it tends to be used.

@duydang2311

Copy link
Copy Markdown
Author

Thank you for the response.
Extension methods added, please have a look!

@sblom

sblom commented Aug 10, 2022

Copy link
Copy Markdown
Owner

Thanks for the pull request!

Is the primary goal to have versions of those methods that don't ever throw?

I notice that the new extraction plan now uses a class's TryParse method, which makes complete sense.

Does anyone know if there are any common types where Parse exists but TryParse doesn't?

@duydang2311

Copy link
Copy Markdown
Author

A regex-matching text could throw any exception when parsing, e.g. OverflowException. So yes, performing an extraction that never throw is the goal of this PR.

If we are talking about the C# system types, all of those having Parse methods will also definitely have the TryParse variants existed.

@oparkerj

Copy link
Copy Markdown
Contributor

I would say to maintain compatibility with user-defined types that use a Parse(string) method, it can look for a TryParse method, and then fallback to the Parse method if one is not found.

@duydang2311

Copy link
Copy Markdown
Author

Thanks for pointing this out. I'll implement an attempt to TryParse before falling back to Parse as soon as possible.

@sblom sblom marked this pull request as draft November 3, 2022 01:01
@sblom

sblom commented Nov 3, 2022

Copy link
Copy Markdown
Owner

@duydang2311, I converted this to a Draft PR for now. Please mark it as Ready for review once it's ready. And let me know if you have any questions or if there's anything I can do to help. Thanks!

@oparkerj

oparkerj commented Nov 3, 2022

Copy link
Copy Markdown
Contributor

Thanks for the pull request!

Is the primary goal to have versions of those methods that don't ever throw?

I notice that the new extraction plan now uses a class's TryParse method, which makes complete sense.

Does anyone know if there are any common types where Parse exists but TryParse doesn't?

Once .NET 7 is available it can also check to see if the type is IParsable<T>

@sblom sblom force-pushed the main branch 7 times, most recently from 119c99a to 18f8b62 Compare December 21, 2023 01:24
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.

3 participants