optional-arguments

Can I default a function argument to the value of __FILE__ at the caller?

别等时光非礼了梦想. 提交于 2019-12-05 04:38:48
In C++, can I have a defaulted argument to a function which defaults to __PRETTY_FUNCTION___ , ___FILE___ , and ___LINE__ as defined at the point of the caller and not the point the defaults are supplied in a header file without using macros? You can't, but you can acheive this behavior with an additional macro. For instance: #DEFINE THROW(e) throwException(e, __FILE__, __LINE__); On a side note, __PRETTY_FUNCTION__ is not standard. No. Macros are expanded at the source line where they occur. You probably can... but definitely not with the restriction you mentioned (no macros). 来源: https:/

Propagating optional arguments

安稳与你 提交于 2019-12-04 18:15:42
问题 The following code does not compile. type A(?arg) = member __.Arg : string option = arg type B(?arg) = inherit A(arg) //ERROR expected type string but has type 'a option I assume this is because an instance of the underlying type of the option must be provided, and the compiler handles passing Some / None based on syntax. Assuming my assumption has been correctly assumed, is there a workaround for this? Is it possible to propagate optional arguments? 回答1: F# spec 8.13.5 Optional arguments to

Python: argparse optional arguments without dashes

馋奶兔 提交于 2019-12-04 16:56:22
问题 I would like to have the following syntax: python utility.py file1 FILE1 file2 FILE2 where file1 and file2 are optional arguments. It is simple to make it working with this syntax: python utility.py --file1 FILE1 --file2 FILE2 using parser.add_argument('--file1',type=file) parser.add_argument('--file2',type=file) however, if I remove the dashes, argparse starts to interprete it as a positional rather than optional argument... In other words, is it possible to specifically tell argparse

R Proper checking of supplied parameters against a list of values?

女生的网名这么多〃 提交于 2019-12-04 05:18:20
问题 In one comment to the accepted answer on how to "correctly" specify optional arguments in R, @LouisMaddox said missing() is useless when you want to use proper checking of supplied parameters against a list though. For a function Foo with parameter bar and optional switch a_or_b (default value "a") you can write Foo <- function(bar, a_or_b=c("a", "b")) ... Is there a proper/recommended/idiomatic way for checking supplied parameters against a list of possible values? I tried to look at

Python: argparse optional arguments without dashes

旧时模样 提交于 2019-12-03 10:06:06
I would like to have the following syntax: python utility.py file1 FILE1 file2 FILE2 where file1 and file2 are optional arguments. It is simple to make it working with this syntax: python utility.py --file1 FILE1 --file2 FILE2 using parser.add_argument('--file1',type=file) parser.add_argument('--file2',type=file) however, if I remove the dashes, argparse starts to interprete it as a positional rather than optional argument... In other words, is it possible to specifically tell argparse whether an argument is optional or positional so that I can have optional parameters without the dashes?

Why is the empty dictionary a dangerous default value in Python? [duplicate]

对着背影说爱祢 提交于 2019-12-03 04:39:18
问题 This question already has answers here : “Least Astonishment” and the Mutable Default Argument (31 answers) Python methods: default parameter values are evaluated ONCE (3 answers) Closed 5 years ago . I put empty braces as the default value for an optional argument to a Python function, and pylint (using Sublime package) told me it was dangerous. Can someone explain why this is the case? And is a better alternative to use None instead? 回答1: It's dangerous only if your function will modify the

Why is the empty dictionary a dangerous default value in Python? [duplicate]

走远了吗. 提交于 2019-12-02 18:40:29
This question already has an answer here: “Least Astonishment” and the Mutable Default Argument 31 answers Python methods: default parameter values are evaluated ONCE 3 answers I put empty braces as the default value for an optional argument to a Python function, and pylint (using Sublime package) told me it was dangerous. Can someone explain why this is the case? And is a better alternative to use None instead? It's dangerous only if your function will modify the argument. If you modify a default argument, it will persist until the next call, so your "empty" dict will start to contain values

Optional arguments and InteropServices

爱⌒轻易说出口 提交于 2019-12-01 10:53:40
I'm using for the firs time optional arguments but I cannot understand difference between those two method definitions: private void method1([Optional, DefaultParameterValue(string.Empty)] string testString) { //do something } private void method2(string testString = "") { //do something } definition of method1 needs: using System.Runtime.InteropServices; Method2 definition is smaller and needs no import. Have I to consider something before using one of those method syntax? Method 1 was present since .NET 1.1. Method 2 was introduced with C# 4 (C# did not support optional parameters until then

Optional arguments and InteropServices

笑着哭i 提交于 2019-12-01 07:32:35
问题 I'm using for the firs time optional arguments but I cannot understand difference between those two method definitions: private void method1([Optional, DefaultParameterValue(string.Empty)] string testString) { //do something } private void method2(string testString = "") { //do something } definition of method1 needs: using System.Runtime.InteropServices; Method2 definition is smaller and needs no import. Have I to consider something before using one of those method syntax? 回答1: Method 1 was

Why does C# allow ambiguous function calls through optional arguments?

做~自己de王妃 提交于 2019-12-01 02:20:52
I came across this today, and I am surprised that I haven't noticed it before. Given a simple C# program similar to the following: public class Program { public static void Main(string[] args) { Method(); // Called the method with no arguments. Method("a string"); // Called the method with a string. Console.ReadLine(); } public static void Method() { Console.WriteLine("Called the method with no arguments."); } public static void Method(string aString = "a string") { Console.WriteLine("Called the method with a string."); } } You get the output shown in the comments for each method call. I