optional-parameters

Why isn't the new() generic constraint satisfied by a class with optional parameters in the constructor?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 17:54:43
问题 The following code fails to compile, producing a "Widget must be a non-abstract type with a public parameterless constructor" error. I would think that the compiler has all of the information it needs. Is this a bug? An oversight? Or is there some scenario where this would not be valid? public class Factory<T> where T : new() { public T Build() { return new T(); } } public class Widget { public Widget(string name = "foo") { Name = name; } public string Name { get; set; } } public class

How do I use parameter overloading or optional parameters in rust?

混江龙づ霸主 提交于 2019-12-01 16:39:35
I am trying to write a print function for a binary tree and here is what I have so far: impl TreeNode { fn print(&self) { self.print(0); } fn print(&self, level: u8) { for _i in range(0,level) { print!("\t"); } match self.data { Some(x) => println!("{}",x), None => () }; match self.left { Some(ref x) => x.print(level+1), None => () }; match self.right { Some(ref x) => x.print(level+1), None => () }; } } I am getting the error: duplicate definition of value print . So I was wondering if there is a way to create functions with the same name but different arguments. Alternatively optional

Using C# delegates with methods with optional parameters [duplicate]

拜拜、爱过 提交于 2019-12-01 15:04:51
This question already has an answer here: Optional parameters on delegates doesn't work properly [duplicate] 4 answers Is there a chance to make this code work? Of course I can make second definition of Foo, but I think it'd be a little non-elegant ;) delegate int Del(int x); static int Foo(int a, int b = 123) { return a+b; } static void Main() { Del d = Foo; } Your delegate asks for exactly one parameter , while your Foo() method asks for at most two parameters (with the compiler providing default values for unspecified call arguments). Thus the method signatures are different, so you can't

Difference between ParameterInfo.DefaultValue and ParameterInfo.RawDefaultValue

回眸只為那壹抹淺笑 提交于 2019-12-01 14:30:30
问题 This is a follow-up question of How do I get default values of optional parameters? From documentation, DefaultValue: Gets a value indicating the default value if the parameter has a default value. This property is used only in the execution context. In the reflection-only context, use the RawDefaultValue property instead. The default value is used when an actual value is not specified in the method call. A parameter can have a default value that is null. This is distinct from the case where

Distinguish &optional argument with default value from no value

家住魔仙堡 提交于 2019-12-01 04:17:53
According to Functions on GigaMonkeys, Common Lisp supports optional positional parameters via &optional and the default value can be set arbitrarily. The default default value is nil . (defun function (mandatory-argument &optional optional-argument) ... ) and the default value can be set arbitrarily (defun function (mandatory-argument &optional (optional-argument "")) ....) Is there a way of distinguishing the cases where the optional parameter has the default value explicitly passed in vs no value at all? EDIT: evidently the page I linked explains this. Occasionally, it's useful to know

Default method parameters in C#

蹲街弑〆低调 提交于 2019-12-01 03:16:22
How can I make a method have default values for parameters? A simple solution is to overload the method: private void Foo(int length) { } private void Foo() { Foo(20); } You can only do this in C# 4, which introduced both named arguments and optional parameters : public void Foo(int x = 10) { Console.WriteLine(x); } ... Foo(); // Prints 10 Note that the default value has to be a constant - either a normal compile-time constant (e.g. a literal) or: The parameterless constructor of a value type default(T) for some type T Also note that the default value is embedded in the caller's assembly

Can't use optional parameters when implementing an interface for a WCF

你说的曾经没有我的故事 提交于 2019-12-01 02:40:24
In my interface I have declared this. [OperationContract] [WebGet] String GetStuff(String beep, String boop = "too lazy to type"); I implemented it as follows. String GetStuff(String beep, String boop = "too lazy to type") { ... } It compiles and uploads as my WCF service. However, when I used it as a web reference and try to execute the code below, I get the compiler whining and weeping about no method with signature of a single parameter. The last line is the problem. How can I then be too lazy to type by default ? ServiceClient client = new ServiceClient(); client.GetStuff("blobb", "not

Distinguish &optional argument with default value from no value

可紊 提交于 2019-12-01 01:40:30
问题 According to Functions on GigaMonkeys, Common Lisp supports optional positional parameters via &optional and the default value can be set arbitrarily. The default default value is nil . (defun function (mandatory-argument &optional optional-argument) ... ) and the default value can be set arbitrarily (defun function (mandatory-argument &optional (optional-argument "")) ....) Is there a way of distinguishing the cases where the optional parameter has the default value explicitly passed in vs

Performance differences between overloading or optional parameters?

妖精的绣舞 提交于 2019-12-01 01:19:45
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"); } private static void test1(string text1, string text2) { Console.WriteLine(text1 + " " + text2); } //

Variable number of method parameters in Objective C - Need an example

人走茶凉 提交于 2019-11-30 23:14:32
From Objective C Programming Guide (Under the "Object Messaging" section), Methods that take a variable number of parameters are also possible, though they’re somewhat rare. Extra parameters are separated by commas after the end of the method name. (Unlike colons, the commas are not considered part of the name.) In the following example, the imaginary makeGroup: method is passed one required parameter (group) and three parameters that are optional: [receiver makeGroup:group, memberOne, memberTwo, memberThree]; I tried to create such a method and it shows an error "Expected ';' after method