Check inside method whether some optional argument was passed

Well, arguments are always passed. Default parameter values just ensure that the user doesn’t have to explicitly specify them when calling the function. When the compiler sees a call like this: ExampleMethod(1); It silently converts it to: ExampleMethod(1, “default string”, 10); So it’s not techically possible to determine if the argument was passed at run-time. … Read more

How to exclude Optional unset values from a Pydantic model using FastAPI?

You can use exclude_none param of Pydantic’s model.dict(…): class Text(BaseModel): id: str text: str = None class TextsRequest(BaseModel): data: list[Text] n_processes: Optional[int] request = TextsRequest(**{“data”: [{“id”: “1”, “text”: “The text 1”}]}) print(request.dict(exclude_none=True)) Output: {‘data’: [{‘id’: ‘1’, ‘text’: ‘The text 1′}]} Also, it’s more idiomatic to write Optional[int] instead of Union[int, None].

How does one declare optional methods in a Swift protocol?

1. Using default implementations (preferred). protocol MyProtocol { func doSomething() } extension MyProtocol { func doSomething() { /* return a default value or just leave empty */ } } struct MyStruct: MyProtocol { /* no compile error */ } Advantages No Objective-C runtime is involved (well, no explicitly at least). This means you can conform … Read more

C# 4.0: Can I use a TimeSpan as an optional parameter with a default value?

You can work around this very easily by changing your signature. void Foo(TimeSpan? span = null) { if (span == null) { span = TimeSpan.FromSeconds(2); } … } I should elaborate – the reason those expressions in your example are not compile-time constants is because at compile time, the compiler can’t simply execute TimeSpan.FromSeconds(2.0) and … Read more

method overloading vs optional parameter in C# 4.0 [duplicate]

One good use case for ‘Optional parameters’ in conjunction with ‘Named Parameters’ in C# 4.0 is that it presents us with an elegant alternative to method overloading where you overload method based on the number of parameters. For example say you want a method foo to be be called/used like so, foo(), foo(1), foo(1,2), foo(1,2, … Read more