All of the new "generic math" interfaces are awesome - long overdue. But please - DON'T implement TryParse as a call to Parse inside a try-catch. One of the key benefits of TryParse is that it does not throw - even internally. Implement Point2d.TryParse as two calls to int.TryParse and return true only if both succeed - no throw/catch inside.
"One of the key benefits of TryParse is that it does not throw - even internally" Could you explain what how that's a benefit? Genuine question. Wouldn't it be functionally the same?
@@amirhosseinahmadi3706 It is just for performance reasons. Returning false is as fast as you can get. Throwing an exception requires much more processing and memory allocations. Of course you have to weigh up whether it's worth optimising your method for your particular use case. But stick to what @Sindrijo has said and 99% of the time it will be the right decision.
I was actually going to comment the same. I do it the other way around. I do the validations during the parse implementation and just short-circuit if anything is wrong and return false. Then Parse just calls that and throws if it fails.
When MS said they will add static interface members, I thought, they lost they mind. But now, that they provided such use cases, I'm changing my opinion :-)
I really like the new Span splitting method, makes me feel in control of my memory. Also i totally love the new abstract interfaces, especially the IParsable, but I probably used INumber a lot more already.
Static abstract methods port interfaces to types beyond the instance of those types. And that is really impressive. With that, the need for the use of reflection is reduced as much as possible.
two points used from some buffer [read i/o etc], and this buffer easy to imaging as array, and then change type of this array, and array type - desired type of structure array
I love these new interfaces and the ability to have static interface methods, and use them IF the code base I'm working on is using the supported runtime.
Question: is there something built in that can do fixed width? Currently i have to do a lot of specific span reads but if there was a range like you had that i could split based upon multiple indexes then that might be better
I'd recommend using a parser combinator library instead. It's IMO by far the easiest and most intuitive way to parse any object, especially considering parsers are monadic, which means you can use LINQ to easily compose parsers.
11:52: is it actually a good idea to throw an exception here? I'd just expect false to be returned here, and the result to be set to the default value. On the caller side, having to wrap a TryParse call within a try / catch sounds like it defeats the point of using TryParse in the first place.
I mentioned this in another reply but no you shouldn’t throw an exception. I did the stupid think of copying from the Parse method but you should return false there
In the case of ints it wouldn't, but for doubles, some locals use period and some use comma as the decimal separator. Not to mention all the different date/time formats.
Awesome! This is actually pretty useful but comes far too late, because i already implemented something similar in our production code bases - implemented in hundres of classes/structs. But when we switch to .NET 7 (we are currently at NET 6), we may drop to this method instead. Wait, what is the '!' operator for s!.Split(',');? WIll this throw an exception when s is null?
In 09:38, why not just do this ? int commaPos = s.IndexOf(','); int first = int.Parse(s.Slice(0, commaPos)); int second = int.Parse(s.Slice(commaPos + 1));
just curious, what are you beginning to type when you are setting a default parameter? You start typing `format = by`, but then you backspace and type null. You did it twice haha
Hi Nick! I'm having kind of a hard time understanding where would you use in real life delegates and events, as we as covariance and contravariance. Do you have anything on your videos on this? Great video as always!
I knew about this, but I didn't know the out parameter of the TryParse methods came after the IFormatProvider parameter. That's pretty dumb IMO, since then you can't have a default null vlue, and the user needs to pass null.. int.TryParse( "20", null, out var value ) vs Int.TryParse( "20", out var value )
About your example - why not just reuse Parse method from TryParse? And in general - is there such use case where logic in Parse and TryParse should be different? Any reason for ms developers to put it on me to implement both methods rather than implement only Parse?
There is a difference actually and I didn't properly show this. TryParse should now throw an exception. Instead it should return either true or false based on whether it could parse the input, so me returning argument exception is wrong there. I should just be returning false. It's just a common .NET pattern.
@@nickchapsas Here's general implementation of TryParse try { result = Parse(s, provider); return true; } catch { result = default; return false; } I think it's just boilerplate and it is place in single extension method for whole codebase, and only Parse method have to be implemented. Maybe you know use case where it makes any sense to implement both?
@@iGexogen He already said the use case, not throwing exceptions. From where did you take this implementation you provided as a 'general implementation'? Because throwing exceptions has a huge cost, it probably is not implemented this way.
You really should overload the method to allow the caller to pass a string, rather than requiring your calling methods to add the .AsSpan() every time. It greatly simplifies your code and does not requie an .AsSpan() function to be called throughout your application.
Meh. Kinda cool, but ultimately not terribly useful, because types rarely only have one string representation. If you want to create a different type for each representation, I guess this would work, but it just seems that writing a static parse function would be quicker and easier to understand. But we’ll see. Maybe I’m just getting old, but adding new language features every month or two seems a bit chaotic, I think. You can’t even start and finish a project before your code is already obsolete. 🙄
I love how random your numbers are
Not watched it yet…. 69 by any chance? 😂
@@MarkCastleand then splits it with comma "6,9"😂
And 4,20
The only numbers that matter are 13 and 42. 😛
The like count of this comment is one of them
All of the new "generic math" interfaces are awesome - long overdue. But please - DON'T implement TryParse as a call to Parse inside a try-catch. One of the key benefits of TryParse is that it does not throw - even internally. Implement Point2d.TryParse as two calls to int.TryParse and return true only if both succeed - no throw/catch inside.
Agree, only implement TryParse with other TryParse. I usually also implement the Parse with the TryParse version of the same type.
I am pretty sure Nick did not do Try Parse as you propose for simplicity reasons. It is not a best practices video.
"One of the key benefits of TryParse is that it does not throw - even internally"
Could you explain what how that's a benefit? Genuine question. Wouldn't it be functionally the same?
@@amirhosseinahmadi3706 It is just for performance reasons. Returning false is as fast as you can get. Throwing an exception requires much more processing and memory allocations. Of course you have to weigh up whether it's worth optimising your method for your particular use case. But stick to what @Sindrijo has said and 99% of the time it will be the right decision.
I was actually going to comment the same. I do it the other way around. I do the validations during the parse implementation and just short-circuit if anything is wrong and return false. Then Parse just calls that and throws if it fails.
When MS said they will add static interface members, I thought, they lost they mind. But now, that they provided such use cases, I'm changing my opinion :-)
we used to use Convert.ChangeType() for primitive data types. now we are changing the implementation to IParsable. its pretty neat.
Hey Nick, I saw you yesterday at Techorama. Thanks for the great talk!
Hey! ❤
I really like the new Span splitting method, makes me feel in control of my memory. Also i totally love the new abstract interfaces, especially the IParsable, but I probably used INumber a lot more already.
Understood everything up to 2:00. After that my brain went coo-coo!
This was a great blend of useful right now, interesting and showing future functionality. Kudos!
10:11 We're circling right back to pre-2000s C, haha
Nice. In the ReadOnlySpan TryParse example. Shouldn't it return false instead of throwing ArgumentOutOfRangeException?
Yes you are right. I copied and pasted it from the Parse method so I totally forgot. Thanks for bringing this up
I appreciate how you use a similar naming scheme to me, semantic-type-as-physical-type, eg FooAsText, FooAsInt etc.
Static abstract methods port interfaces to types beyond the instance of those types. And that is really impressive. With that, the need for the use of reflection is reduced as much as possible.
Okay maybe I need to rewatch this later after a cup of coffee or two... looks like something useful to fully understand.
Great! I am watching your video since last month. I have completly changed the way of how I write code. Thank you very much Nick.
two points used from some buffer [read i/o etc], and this buffer easy to imaging as array, and then change type of this array, and array type - desired type of structure array
Great video. One thing that I was missing was to introduce the [NotNullWhen(true)], but maybe that is the separate topic itself
I love these new interfaces and the ability to have static interface methods, and use them IF the code base I'm working on is using the supported runtime.
I very much like this feature, I would rather prefer the "shapes" approach but this is really very very very userful
i use 'is' syntax and return an Option
Not sure If I am going to use this technique, but I did learn about static abstract members now, which I didn't know about before.
Good practice for csv line parsing
Does System.Text.Json automatically use the IParseable interface? I'd love to get rid of som of the JsonConverters I've had to build.
Thanks!
Very useful, thanks.
Question: is there something built in that can do fixed width? Currently i have to do a lot of specific span reads but if there was a range like you had that i could split based upon multiple indexes then that might be better
I'd recommend using a parser combinator library instead. It's IMO by far the easiest and most intuitive way to parse any object, especially considering parsers are monadic, which means you can use LINQ to easily compose parsers.
Does somebody know if Complete Nick Chapsas Course Bundle will get all future courses? Please ☺ I'm thinking of buying it...
Awesome ty. This is waaaaay overdue. Doing this with generics is painful and not really possible
Hey Nick. What software are you using to do your screen presentation with your picture as part of the production?
I didn't see a reply, but it looks like the type of thing that can be done in OBS Studio.
Nice. Wouldn't call that extension "Parse" though, since the string isn't parsing, it's being parsed.
ParseTo would be a better one
@@nickchapsas Or ParseMe
Beparsed (similarly to "begone")
"Parse" makes sense for me, it's like -> parse as
Favorite thumbnail
The span part is imo way more interesting than the interface and abstract static stuff
11:52: is it actually a good idea to throw an exception here? I'd just expect false to be returned here, and the result to be set to the default value. On the caller side, having to wrap a TryParse call within a try / catch sounds like it defeats the point of using TryParse in the first place.
I mentioned this in another reply but no you shouldn’t throw an exception. I did the stupid think of copying from the Parse method but you should return false there
How does the IFormatProvider play into your custom parse logic?
In the case of ints it wouldn't, but for doubles, some locals use period and some use comma as the decimal separator. Not to mention all the different date/time formats.
This can be cool ! Thanks ! ❤❤
Heck yeah! The puns are back! ❤
Is there anything that would be similarly performant for binary parsing?
Awesome! This is actually pretty useful but comes far too late, because i already implemented something similar in our production code bases - implemented in hundres of classes/structs.
But when we switch to .NET 7 (we are currently at NET 6), we may drop to this method instead. Wait, what is the '!' operator for s!.Split(',');? WIll this throw an exception when s is null?
You're telling the compiler: "s is never null, trust me, so stop giving me those null reference warnings..."
In 09:38, why not just do this ?
int commaPos = s.IndexOf(',');
int first = int.Parse(s.Slice(0, commaPos));
int second = int.Parse(s.Slice(commaPos + 1));
just curious, what are you beginning to type when you are setting a default parameter? You start typing `format = by`, but then you backspace and type null. You did it twice haha
Why record, not a strict? What are the advantages and when to use which?
records are immutable
Record is reference type, like class.
He could have used record struct too.. It's just an example for the parser, so he probably didn't bother. Brevity.
"Random number"
Casually type in "69"
😂😂
Benchmarks?
Hi Nick! I'm having kind of a hard time understanding where would you use in real life delegates and events, as we as covariance and contravariance. Do you have anything on your videos on this? Great video as always!
I wonder why it's not spelled IParseable like ICloneable?
Nice "random" number you chose 😳🤣
I really wish there were a GetParse(inputString, optional DefaultValue ) where failures return the default value.
I'm probably missing something, but what prevents you to create an extension method just like that?
@@tekozlofiu I actually already have, but I cannot share them.
Nice
I knew about this, but I didn't know the out parameter of the TryParse methods came after the IFormatProvider parameter. That's pretty dumb IMO, since then you can't have a default null vlue, and the user needs to pass null..
int.TryParse( "20", null, out var value )
vs
Int.TryParse( "20", out var value )
nice
Just a random number Nick 😉
why number 69? it's not a random number at all...hmm
Totally random number, there is no any meaning in it :D
@@valera924 yup. Like (4,20), or 80085 that he also randomly chooses. I mean, it is random, but there's fewer options than a 4 sided die. 😆
When Nick says Random number it means always 69
❤
About your example - why not just reuse Parse method from TryParse? And in general - is there such use case where logic in Parse and TryParse should be different? Any reason for ms developers to put it on me to implement both methods rather than implement only Parse?
There is a difference actually and I didn't properly show this. TryParse should now throw an exception. Instead it should return either true or false based on whether it could parse the input, so me returning argument exception is wrong there. I should just be returning false. It's just a common .NET pattern.
@@nickchapsas Here's general implementation of TryParse
try
{
result = Parse(s, provider);
return true;
}
catch
{
result = default;
return false;
}
I think it's just boilerplate and it is place in single extension method for whole codebase, and only Parse method have to be implemented. Maybe you know use case where it makes any sense to implement both?
@@iGexogen better do it the opposite way: if TryParse returns false - throw
@@iGexogen
He already said the use case, not throwing exceptions.
From where did you take this implementation you provided as a 'general implementation'? Because throwing exceptions has a huge cost, it probably is not implemented this way.
6:04 hehe ass pan
Of course the number input used as an example is 69.
Just parsing the time
"random number"
You really should overload the method to allow the caller to pass a string, rather than requiring your calling methods to add the .AsSpan() every time. It greatly simplifies your code and does not requie an .AsSpan() function to be called throughout your application.
Noice number
"Just random number" 😅
int.Paws? int.Pause? 😅
They got lazy and "forgot" to implement IParseable and IBitwiseOperators for enum types.
Meh. Kinda cool, but ultimately not terribly useful, because types rarely only have one string representation.
If you want to create a different type for each representation, I guess this would work, but it just seems that writing a static parse function would be quicker and easier to understand.
But we’ll see. Maybe I’m just getting old, but adding new language features every month or two seems a bit chaotic, I think. You can’t even start and finish a project before your code is already obsolete. 🙄
.NET 8 preview 5, really?
Lmao 1 yr later .net9 is in use.
A type should not implement its own serialization.
Pretty marginal stuff IMO
Kinda weird stuff
We are still using Framework 4.8 😥
We are slowly moving to .net8, though
bro you lost me 50 million "if we go into that"s ago