C
C#Banana Poop

✅ Using `Func<ReadOnlySpan<char>,bool>` but not allowed due to Pointer Type

I am building a parser, and performance is of concern. So instead of allocating infinite strings whenever I need to take a substring. I'd logically prefer to just take slices of the same buffer using Spans. In addition I want to use Func<ReadOnlySpan<char>,bool> to do some work in helping me parse, which would take in the Span to do some work. However, this is illegal in C# Compiler Error CS0306 due to ReadOnlySpan<char> being a pointer type readonly ref struct. I have considered just making a type, with one function on it, and overriding that in subtypes to get around it, but this is really not suitable. I may for instance wish to compile LINQ expressions at run time, to server as parsers. This has got me stuck since the 'correct' way is illegal, and any other way, is highly comprimised. Thoughts?
C
canton746d ago
Can you just define your own delegate type? public delegate bool MyDelegate(ReadOnlySpan<char> input); or so?
BP
Banana Poop46d ago
Hmm looks like it works
C
canton746d ago
The problem is with ref struct types being used as generic type parameters. The compiler has rules around what it can and can't do with ref structs, but if all it sees is a generic T, it can't know what special rules apply when T is a ref struct (and Func is, of course, a generic type)
BP
Banana Poop46d ago
Yes this works completly, I thought for some reason whatever lambda used to define this, would also complain, but it works perfectly. I have never needed to use delegates before (except when interfacing with older style code) so it's interesting to me that they still have uses and aren't fully superceded by Func and Action
C
canton746d ago
Yeah, anything with ref / out needs a custom delegate type as well. And quite often it's just clearer, as you get to name your parameters, rather than having a fairly horrible Func<string, string, bool, bool, bool> etc (Func<T> is, after all, just a convenience delegate T Func<T>() defined in the BCL) https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Function.cs
BP
Banana Poop46d ago
Thanks @canton7 this solves everything, and delegate is mucher nicer here (even if it wasn't required, which it is). I may start using my own delegates elsewhere, since it's good for when something isn't just a throw-away type for a random odd job (like value tuples vs record) I guess
C
canton746d ago
Yeah, the downside is that all of linq uses Action/Func, and converting between delegate types is a little bit cumbersome Plus var foo = (int x) => { } is automatically inferred as Action<int> these days. So Action/Func are still a little bit special But, the upsides of being able to name/document your parameters often outweigh that $close
M
MODiX46d ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server
More Posts
concurrency and atomicity Asp.net core web api and efcorelet's say there's an endpoint called AssingUser() ,this endpoint assigns one donation to this currenBuilding a generic repository for mongodbhello folks I was thinking build a generic repository structure for mongodb. but someday if i want Reading large xml file from archive by using XmlReader in Parallel modeHello 👋. I am looking for how can I read data from archive xml file in Parallel mode. I have archiResizing the Console window - Console ApplicationHi there, I'm trying to resize the console window in a Console Application. I have tried multiple ✅ Creating a datetime with a timezoneHow do I create a `DateTime` with `new DateTime(2010, 05, 12);` and give it a specific `TimeZoneInfoCustom Newtonsoft JSON deserializationI have a dictionary where: - The key will always be a string - The value will be either: - string Issue with Form.Close() in WinformsI have a custom form called CustomMessageBox In that form, I have a method that defines a button clIssue with IMGUI.Net.Hello, I am creating an ImGUI application and It doesn't let me move the ui across my entire screen,Issue with deserializing Enums in saved JSONHey, I've been trying to deserialize a list of cards I serialized in C#, the issue I'm having is it From HttpClient, can I save/copy the request to share with someone?Is there a way to copy raw request details? For example, I'm looking for a similar output like: -3D Unity RaycastingHi guys, currently working on a puzzle game involving lasers and mirrors, however, have hit a brick Computer Science - Computer ArchitectureOn a 64 bit system (where the bus length and word size are 64 bits) how would we determine the ammouConnecting SQLite in MAUIHow to connect the database without absolute path? If with it, the program works only on the computeDifferent HttpClient headersWhen I make an HTTP request in my browser (Chrome), I get different response headers than I do in myHttpRequestException:An error occurred while sending the request. WebExceptionHttpRequestException:An error occurred while sending the request. WebException:Unable to connect to ✅ EF does not put datetimes properly to the datebaseI am writing a backend app in asp.net core. This is my service code : ```c# public virtual async TaShadow Prop Error with IdentityDbContextThis is my IdentityDbContext: public class ApplicationUser : IdentityUser { [Required(ErrorMessWpf custom button template probleHi! I am trying to create custom button control, but I have a problem with a content property. ButtoWinfoms problem in Hosts fileI am writing a mini site blocker, the program has 2 functions to block the site and add the site thaDTO validation with DRY principle (without repeating the same validation)Hello, I would like to know what is used in practice, regarding the DTO validation. Let's say for 2