optional-parameters

Default arguments vs overloads, when to use which

二次信任 提交于 2019-11-29 13:47:45
In Kotlin there are two ways to express an optional parameter, either by specifying default argument value: fun foo(parameter: Any, option: Boolean = false) { ... } or by introducing an overload: fun foo(parameter: Any) = foo(parameter, false) fun foo(parameter: Any, option: Boolean) { ... } Which way is preferred in which situations? What is the difference for consumers of such function? Jayson Minard In Kotlin code calling other Kotlin code optional parameters tend to be the norm over using overloads. Using optional parameters should be you default behavior. Special cases FOR using defaulted

F#: Why can't I use optional parameters in loose functions?

≡放荡痞女 提交于 2019-11-29 13:26:45
Why can't I use optional parameters in loose functions defined with "let"? Why are they only allowed in member functions? I suspect they are provided only for compatibility with .NET functions. They aren't something you encounter in functional languages. The problem with an optional parameter is you can't curry it. If a function f's second parameter is optional, what is let g = f x ? Is it a function taking one argument, or a value obtained by evaluating f on x plus the default second parameter? It is allowed in OCaml. Therefore it should be possible to implement it in F#. http://caml.inria.fr

Using an enum as an optional parameter

别等时光非礼了梦想. 提交于 2019-11-29 11:25:05
问题 I have several methods in an application I'm working on loaded with optional parameters, some of which are enums. Currently, in order to do that I'm writing methods with a similar type of signature: public void SomeMethod(string myFirstParam = "", string mySecondParam = "", MyEnum myThirdParam = (MyEnum )(-1)){ if (myThirdParam != (MyEnum ) (-1)) { //do something with it } } So my first question is, is there some pitfall to this approach I haven't realized, but in time will become painfully

Named and optional parameters, and WCF

大兔子大兔子 提交于 2019-11-29 09:09:04
so .Net 4 added named and optional parameters which are pretty sweet. I don't need to make as many 1 line overload methods. Will that work over WCF? Since these are compiler semantics I'd say no. However you'd expect them to work in the only following way. On the Service Code side all code would accept the defaulted parameters. On the client side I note that the 'Add Service Reference' tooling on VS2010 doesn't take the defaults and add them to the generated proxy. So You'd have to generate you're own proxy. In this way the client code can use the defaults if the the default is specified in

Is it possible to use optional/default parameters in a lambda expression in c#?

泄露秘密 提交于 2019-11-29 09:06:32
Is there a way to use optional arguments (default parameters) with lambda expressions in c#? I have read through the documentation but can find nothing to say one way or the other. To illustrate, I can define simple method that uses an optional argument to supply a default value like so: void MyMethod(string arg = "default-value") { Console.WriteLine(arg); } What I want to know is if I am able to do the same thing using a lambda expression. // gives a syntax error Action<string> MyMethod = (arg = "default") => Console.WriteLine(arg); I can work in an optional parameter with a default value

C# Default Parameters

ε祈祈猫儿з 提交于 2019-11-29 08:14:45
问题 This is, probably, a very simple answer for someone. I have a method with an Optional Parameter like so; public static Email From(string emailAddress, string name = "") { var email = new Email(); email.Message.From = new MailAddress(emailAddress, name); return email; } Now, I must target .Net 3.5 and it was my understanding that Optional Parameters are part of .Net 4. However, my project builds and I double checked the Properties - Application page which states 3.5 as the target framework.

call C# method from powershell without supplying optional arguments

北慕城南 提交于 2019-11-29 07:32:45
I have a c# method I am loading from a dll with optional string arguments that default to null . For example public void foo(string path, string new_name = null, bool save_now = true) { if(name == null) new_name = Path.GetFileNameWithoutExtension(path); ... if(save_now) Save(); } I want to call this from within a powershell script and not supply a value for new_name but one for save_now . As per this seemingly very similar question I have tried $default = [type]::Missing $obj.foo($path, $default, $false) but this results in new_name being set as "System.Reflection.Missing" within the function.

Why optional parameters must appear at the end of the declaration

佐手、 提交于 2019-11-29 05:39:05
In all programming languages supporting optional parameters that I have seen there is a imitation that the optional parameters must appear at the end of the declaration. No required parameters may be included after an optional item. What is the reason for that ? I guess it can be compiler/interpreter requirement. Well, if they were at the front, how would you detect when they've stopped being supplied? The only way would be if the variable type was different after the optional parameters. Bit of a weird requirement, so it makes sense that you just force them to be last (save the trouble of

How to skip optional parameters in C#?

三世轮回 提交于 2019-11-29 05:24:35
Example: public int foo(int x, int optionalY = 1, int optionalZ = 2) { ... } I'd like to call it like this: int returnVal = foo(5,,8); In other words, I want to provide x and z , but I want to use the default for Y , optionalY = 1. Visual Studio does not like the ,, Please help. Josiah Ruddell If this is C# 4.0, you can use named arguments feature: foo(x: 5, optionalZ: 8); See this blog for more information. In C# 4.0 you can name the arguments occurring after skipped defaults like this: int returnVal = foo(5, optionalZ: 8); This is called as named arguments . Several others languages provide

Optional parameters in Python functions and their default values [duplicate]

纵饮孤独 提交于 2019-11-29 04:41:53
问题 Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm kind of confused about how optional parameters work in Python functions/methods. I have the following code block: >>> def F(a, b=[]): ... b.append(a) ... return b ... >>> F(0) [0] >>> F(1) [0, 1] >>> Why F(1) returns [0, 1] and not [1] ? I mean, what is happening inside ? 回答1: Good doc from PyCon a couple years back - Default parameter values explained. But basically, since lists are mutable objects, and