RegExtract icon indicating copy to clipboard operation
RegExtract copied to clipboard

feat: add Try-prefixed methods

Open duydang2311 opened this issue 3 years ago • 8 comments

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

duydang2311 avatar Jul 28 '22 19:07 duydang2311

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

oparkerj avatar Aug 10 '22 08:08 oparkerj

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

duydang2311 avatar Aug 10 '22 12:08 duydang2311

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?

sblom avatar Aug 10 '22 15:08 sblom

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.

duydang2311 avatar Aug 10 '22 16:08 duydang2311

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.

oparkerj avatar Aug 10 '22 21:08 oparkerj

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

duydang2311 avatar Aug 11 '22 14:08 duydang2311

@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!

sblom avatar Nov 03 '22 01:11 sblom

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>

oparkerj avatar Nov 03 '22 01:11 oparkerj