question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Why overloads instead of optional parameters?

See original GitHub issue

Edit:

I’m giving the FSharp.Data.GraphQL client provider another try, since it’s been a few months. Looks like I’m not running into the same errors I got last time.

One thing I notice is that when I create an operation using a query like so:

let op = B.Operation<"""            query ($uId:Int, $rId:Int, $sinceTimestamp:String, $first:Int, $offset:Int) {
              answer(id: $rId) {
                agreed_users (sinceTimestamp:$sinceTimestamp, first:$first, offset:$offset) {
                    id
                    is_verified
                    username
                    avatar
                    follows_status(uId: $uId)
                }
              }
            }""">()

op.Run creates an overload for every permutation of parameters, plus a runtime context.

Is there any reason optional parameters were not used instead? Seems like it would be much easier to read and use, and probably more performant too. If there’s no particular reason, is that something you would consider?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
johnberzy-bazingacommented, Mar 24, 2020

@NatElkins one option is to just generate our methods with explicit options rather than creating all the overloads. Maybe even providing both options and parameterize the new behaviour.

type MyApi = GraphQLProvider<"schema.graphql", explicitOptionalParameters=true>
let input = MyApi.Types.MyInput(x = "required", y = Some "optional")
1reaction
iveltencommented, Nov 12, 2019

@NatElkins as you said, yes, we originally designed type constructors to use optional parameters for their properties. But we faced a problem with the Type Provider SDK itself and nullable/option types.

The SDK does not allow us to use an option or nullable type as an optional argument when declaring a provided constructor - at least not if we don’t provide a default value for it. But the design around it does have its own complications. I opened an issue in the Type Provider SDK repository that explains better this problem.

Basically, if we create an optional argument, we need to provide a default value. And if the optional argument is a value type, it does not have an “empty” definition, so there is no way to know if the user passed or not a value. Even when we get the default value, there is no way to determine if this value came as an omission of the optional parameter by the user, or if this value actually came of the user providing it.

Using a practical example, if the optional argument is an int type, and the default value is 0, when the user does not provide a value, we can’t say if 0 was given by the user or is just the default value that came by the omission of a value in the optional parameter. So, in the end, we would not be able to know if the optional property, which is an int option, should receive Some 0 or None.

Based on this issue, we decided that overloads would be the best workaround. As optional parameters are treated as overloads in the IL, we thought that in practice, the result of it running would not change. I also did test it with around 10.000 overloads and did not notice an impact at the time.

If this problem turns things unusable, I think we would need to think about the other workaround in the issue. Or discuss any other alternative that we did not think of at the time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

method overloading vs optional parameter in C# 4.0
It makes perfect sense that if the method body is the same, then use optional params, but if the method body will vary...
Read more >
Method overloads vs optional parameters in C# | ...
Optional Parameters and Method Overloads are simply tools designed to help you be more expressive in your code that other developers will be ......
Read more >
"Modern" C#: Is Method Overloading or Optional ...
Stick to overloads. It's cleaner and is more testable. Optionals, especially if they are added later are in actual fact a breaking change...
Read more >
C# Optional Parameters vs. Method Overloading
C# Optional Parameters vs. Method Overloading. I was recently working on some data access/repository code and came across a nice generic ...
Read more >
Overload or Optional Parameters
When I have a function that might, or might not receive a certain parameter, is it better to overload the function, or to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found