Simplify ignore return values #1162
Replies: 5 comments 9 replies
-
These methods don't return unit because they are fluent interfaces. WebApplication.CreateBuilder().Build()
.UseResponseCompression()
.UseHttpsRedirection()
.UseRouting()
.UseSwagger()
.UseSwaggerUI()
.UseAuthentication()
.UseAuthorization()
.UseEndpoints(fun endpoints -> endpoints.MapControllers() |> ignore)
|> fun app ->
if not (app.Environment.IsProduction()) then
app.UseDeveloperExceptionPage() |> ignore |
Beta Was this translation helpful? Give feedback.
-
@lucasteles I don't disagree the interop pain exists, especially minimal api feels terrible to use because of it.
type IEndpointRouteBuilder with
member builder.MapTodo() : IEndpointConventionBuilder =
// Todos
builder.MapGet("/todo", ...) |> ignore
builder.MapGet("/todo/{todoId:int}", ...) |> ignore
builder
.MapPost("/todo", ...)
.RequireAuthorization() |> ignore
builder
.MapDelete("todo/{todoId:int}", ...)
.RequireAuthorization() |> ignore I worked around it with a custom |
Beta Was this translation helpful? Give feedback.
-
In case anyone else stumbles across this and wants another option for a workaround, one approach is to add this to the file in question. #nowarn "20" |
Beta Was this translation helpful? Give feedback.
-
I agree with @Happypig375, because the code can be translated from f# to C#, or vice versa. The use of prepositional grammar let builder = WebApplication.CreateBuilder()
// hided
let app = builder.Build()
if not (app.Environment.IsProduction()) then
app.UseDeveloperExceptionPage() |> ignore
let _ = app.UseResponseCompression()
let _ = app.UseHttpsRedirection()
let _ = app.UseRouting()
let _ = app.UseSwagger()
let _ = app.UseSwaggerUI()
let _ = app.UseAuthentication()
let _ = app.UseAuthorization()
// double ignore
let _ = app.UseEndpoints(fun endpoints -> endpoints.MapControllers() |> ignore) |
Beta Was this translation helpful? Give feedback.
-
C# and F# is two different domain, if one needed to work having returns while the others dislike it, then a layer to glue the two domain together is supposed to be implemented by you |
Beta Was this translation helpful? Give feedback.
-
The problem
When working with interop code it's common to have a
|> ignore
on almost every line and every callback action. Eg:Simple EF case:
Basically, any other .NET lib not made for F# is like
And when
Action<T>
it is worse, formatting is pain, and legibility is warmedThis pattern makes porting simple code into a very cumbersome translation, with lots of difficulty for people who are familiarized with default .NET APIs to just get in in F#
Putting the
ignore
at the front of some expressions could make them simple/small, but I feel that it gives a wrong meaning to the expression like it should not important at all, it's more a semantic issueSo, what could be a solution?
I think that disabling the warnings for a whole file would not be a good solution,
ignore
would still be necessary forActions<T>
and we would lose valid cases which we don't want to ignore valuesI believe this is a paper cut case that is so common that would be healthy to have an alternative
I can think 2 suggestions to make this less noisy
Postfix ignore operator
With this, we would still have to ignore returns explicitly, but with priority and not affecting legibility that much.
Ignore block scope
A block that doesn't complain about not
unit
return valuesI honestly prefer the first approach, but the idea here is to discuss this.
Workaround
Today a way to get around this is to put the expressions in a list and ignore it, which looks more like a kludge than a proper solution
Beta Was this translation helpful? Give feedback.
All reactions