Closed CrispyDrone closed 4 years ago
Hi! Glad you enjoyed my talk - thanks for the note, much appreciated.
The reason Superpower parsers aren't covariant is that structs in C# don't support it. A Result<T>
struct can't declare T
as out
the way an interface can. Superpower uses struct results to reduce allocations - there are a lot of results generated during the parsing process - and covariance was sacrificed as a result, unfortunately.
Cheers, Nick
Hi Nick,
Thanks for your quick response. I see, I wasn't actually aware that you could only make interfaces or delegates co- and contravariant.
And I think the lack of it can easily be circumvented by using for example: MapFilter.Select(mapFilter => mapFilter as Filter);
which isn't that cumbersome.
I'm looking forward to experimenting much more with this library!
Hello,
I'm trying out the library for the first time after having watched your great talk about "Building a parser in C#, from first principles".
While writing the following code, I noticed this didn't compile with the following error:
MapFilter
is a class that derives fromFilter
.In Sprache I see that a parser is defined as
public delegate IResult<T> Parser<out T>(IInput input);
However in superpower a text parser is defined aspublic delegate Result<T> TextParser<T>(TextSpan input);
If I'm correct this means that in superpower I can't make use of covariance which is why the code won't compile.
Is there specific reason for this?
Cheers