optional-parameters

Performance differences between overloading or optional parameters?

非 Y 不嫁゛ 提交于 2019-11-30 20:16:22
问题 No, not answered there ↑ I wonder if I should be using optional parameters in C#. Until now I was always overloading methods. But optional parameters are nice too, cleaner and less code. And I use them in other languages so I'm also used to them in some way. Is there anything that speaks against using them ? Performance is the first key point for me. Would it drop ? Example code: class Program { // overloading private static void test1(string text1) { Console.WriteLine(text1 + " " + "world");

C# Optional Parameters or Method Overload? [duplicate]

半城伤御伤魂 提交于 2019-11-30 14:33:26
问题 This question already has answers here : method overloading vs optional parameter in C# 4.0 [duplicate] (11 answers) Closed 6 years ago . Since C# added optional parameters is it considered a better practice to use optional parameters or method overloads or is there a particular case where you would want to use one over the other. i.e a function w/ lots of parameters would be better suited w/ optional parameters? 回答1: Optional parameters are nice, but should be used when it makes sense.

Fortran 2003 / 2008: Elegant default arguments?

懵懂的女人 提交于 2019-11-30 13:13:25
In fortran, we can define default arguments. However, if an optional argument is not present, it can also not be set. When using arguments as keyword arguments with default values, this leads to awkward constructs like PROGRAM PDEFAULT CALL SUB CALL SUB(3) CONTAINS SUBROUTINE SUB(VAL) INTEGER, OPTIONAL :: VAL INTEGER :: AVAL ! short for "actual val" IF(PRESENT(VAL)) THEN AVAL = VAL ELSE AVAL = -1 ! default value END IF WRITE(*,'("AVAL is ", I0)') AVAL END SUBROUTINE SUB END PROGRAM PDEFAULT Personally, I often ran into the problem of accidentially typing VAL instead of AVAL , i.e. the

Method Overriding and Optional Parameters

依然范特西╮ 提交于 2019-11-30 13:08:35
问题 Would someone care to explain how this code produces the folowing output? using System; namespace ConsoleApplication1 { class Test { public override string ToString() { return "ToString override"; } public string ToString(string optional = "") { return String.Format("ToString with optional parameter {0}", optional); } } class Test2 { public new string ToString() { return "ToString new"; } public string ToString(string optional = "") { return String.Format("ToString with optional parameter {0}

copy constructor with default arguments

天大地大妈咪最大 提交于 2019-11-30 11:32:49
As far as I know, the copy constructor must be of the form T(const T&) or T(T&) . What if I wanted to add default arguments to the signature? T(const T&, double f = 1.0); Would that be standards compliant? Yes. §[class.copy]/2: A non-template constructor for class X is a copy constructor if its first parameter is of type X& , const X& , volatile X& or const volatile X& , and either there are no other parameters or else all other parameters have default arguments [ Example: X::X(const X&) and X::X(X&,int=1) are copy constructors. You can just create two different constructors: T(const T&) T

System.MissingMethodException after adding an optional parameter

浪子不回头ぞ 提交于 2019-11-30 10:55:36
I am getting error of System.MissingMethodException after I have an optional parameter in one component and the other component which call it was not build as it call it with old number of parameters. Only component in which parameter is added was build an deployed as patch. The calling component is old as there is no change in it. When the calling component run it gives error : Exception Information Exception Type: System.MissingMethodException Message: Method not found: 'LabelURLs IPSD.BnB.Transaction.Postage.GetLabelURLs(System.String)'. Data: System.Collections.ListDictionaryInternal

Python: how to check whether optional function parameter is set

依然范特西╮ 提交于 2019-11-30 10:40:39
Is there an easy way in Python to check whether the value of an optional parameter comes from its default value, or because the user has set it explicitly at the function call? Lot of answers have little pieces of the full info, so I'd like to bring it all together with my favourite pattern(s). default value is a mutable type If the default value is a mutable object, you are lucky: you can exploit the fact that Python’s default arguments are evaluated once when the function is defined (some more about this at the end of the answer) This means you can easily compare a default mutable value

A javascript design pattern for options with default values?

断了今生、忘了曾经 提交于 2019-11-30 10:25:24
问题 // opt_options is optional function foo(a, b, opt_options) { // opt_c, opt_d, and opt_e are read from 'opt_options', only c and d have defaults var opt_c = 'default_for_c'; var opt_d = 'default_for_d'; var opt_e; // e has no default if (opt_options) { opt_c = opt_options.c || opt_c; opt_d = opt_options.d || opt_d; opt_e = opt_options.e; } } The above seems awfully verbose. What's a better way to handle argument options with default parameters? 回答1: This uses jQuery.extend but could be

Default arguments vs overloads, when to use which

佐手、 提交于 2019-11-30 09:15:25
问题 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? 回答1: In Kotlin code calling other Kotlin code optional parameters tend to be the norm over using

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

旧时模样 提交于 2019-11-30 09:09:58
问题 Why can't I use optional parameters in loose functions defined with "let"? Why are they only allowed in member functions? 回答1: 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