polyadic / funcky

Funcky is a Functional Library for C#
https://polyadic.github.io/funcky
Apache License 2.0
19 stars 4 forks source link

Maybe allow TryGetValue in razor Context. (if we can determine that somehow) #689

Closed FreeApophis closed 2 years ago

FreeApophis commented 2 years ago

Example:

@if(Error.TryGetValue(out var error))
{
    <div class="error _text-small">
        @error.Message
    </div>
}

The alternatives are pretty bleak, because Select or AndThen are not really usable.

bash commented 2 years ago

Razor files generate C# code, which is then analyzed by our analyzer. We should probably be able to detect where the generated source comes from?

On another note, we might also be able to provide some convenience for directly inserting Option values into the document. \ @expression translates to a call to RenderTreeBuilder.AddContent(index, value) I think it should be possible to extend this with extension methods for Option values.

With that approach it should even be possible to do:

@option.Select<RenderFragment>(value => @<div>@value</div>);
bash commented 2 years ago

Sadly my approach with the extension method doesn't work because the built-in overload with object always wins against my extension method.

TryGetValue it is then :)