optional-arguments

C++ messed up: optional argument forced to be mandatory

筅森魡賤 提交于 2019-12-11 17:29:19
问题 I want to have a function with optional argument. However, VC++ is throwing an error I cannot comprehend. The code is as simple as follows: #include <iostream> #include <iomanip> #include <string> using namespace std; void myFunction(string something, int w); int main() { myFunction("a string"); int a = 1; } void myFunction(string something, int w = 5) { cout << something << " - " << w << endl; } It complains: 'myFunction': function does not take 1 arguments However if I move myFunction()

Assigning generic to rest argument and having optional / default arguments

人盡茶涼 提交于 2019-12-11 17:00:02
问题 I am trying to create this function below that takes one optional argument. It's an object, and the only prop needed is value however, I am looking to pass along any ...rest parameters that area also passed in. const useHook = <T extends { value: string }>({ value: v = '', ...props }: T = { value: '' } as T) => { const defaultValue = React.useMemo(() => v, [props]) const [value, setValue, resetValue] = Hooks.useState(defaultValue) const onChange = React.useCallback((e) => setValue(e.target

Optional argument in a method with ocaml

北慕城南 提交于 2019-12-11 13:00:14
问题 I encounter a problem with a optional argument in a method class. let me explain. I have a pathfinding class graph (in the Wally module) and one his method shorthestPath . It use a optional argument. The fact is when I call (with or not the optional argument) this method OCaml return a conflict of type : Error: This expression has type Wally.graph but an expression was expected of type < getCoor : string -> int * int; getNearestNode : int * int -> string; shorthestPath : src:string -> string

Non-Standard Optional Argument Defaults

别等时光非礼了梦想. 提交于 2019-12-10 16:27:43
问题 I have two functions: def f(a,b,c=g(b)): blabla def g(n): blabla c is an optional argument in function f . If the user does not specify its value, the program should compute g(b) and that would be the value of c . But the code does not compile - it says name 'b' is not defined. How to fix that? Someone suggested: def g(b): blabla def f(a,b,c=None): if c is None: c = g(b) blabla But this doesn't work. Maybe the user intended c to be None and then c will have another value. 回答1: def f(a,b,c

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

帅比萌擦擦* 提交于 2019-12-09 02:32:54
问题 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

How to deal with ellipsis (…) in the presence of optional arguments?

好久不见. 提交于 2019-12-07 10:06:41
问题 I have a problem with ellipsis when I use optional arguments in my function definition. To clarify, I define following functions: func1 <- function (x) (x-2)^2 func3 <- function (fun, arg.curve.user){ arg.curve.user$expr <- substitute(func1) arg.curve.default <- list(col = "blue", n = 1000, main = "This is a test") arg.curve <- modifyList (arg.curve.default, arg.curve.user) do.call("curve", arg.curve) } # optimizes func1 and call func2 to plot func1 func2 <- function (lb, ub, n.restarts = 5,

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

ε祈祈猫儿з 提交于 2019-12-07 01:22:59
问题 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? 回答1: 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. 回答2: No. Macros are expanded at the source line where

argparse doesn't check for positional arguments

白昼怎懂夜的黑 提交于 2019-12-06 06:04:38
I'm creating a script that takes both positional and optional arguments with argparse. I have gone through Doug's tutorial and the python Docs but can't find an answer. parser = argparse.ArgumentParser(description='script to run') parser.add_argument('inputFile', nargs='?', type=argparse.FileType('rt'), parser.add_argument('inputString', action='store', nargs='?') parser.add_argument('-option1', metavar='percent', type=float, action='store') parser.add_argument('-option2', metavar='outFile1', type=argparse.FileType('w'), parser.add_argument('-option3', action='store', default='<10', args =

How to deal with ellipsis (…) in the presence of optional arguments?

二次信任 提交于 2019-12-05 16:24:58
I have a problem with ellipsis when I use optional arguments in my function definition. To clarify, I define following functions: func1 <- function (x) (x-2)^2 func3 <- function (fun, arg.curve.user){ arg.curve.user$expr <- substitute(func1) arg.curve.default <- list(col = "blue", n = 1000, main = "This is a test") arg.curve <- modifyList (arg.curve.default, arg.curve.user) do.call("curve", arg.curve) } # optimizes func1 and call func2 to plot func1 func2 <- function (lb, ub, n.restarts = 5, n.sim = 10, ...){ arg.curve.user <- as.list(substitute(list(...))) output <- gosolnp(fun = func1, LB =

Multiple optional arguments python

て烟熏妆下的殇ゞ 提交于 2019-12-05 07:59:53
So I have a function with several optional arguments like so: def func1(arg1, arg2, optarg1=None, optarg2=None, optarg3=None): Optarg1 & optarg2 are usually used together and if these 2 args are specified then optarg3 is not used. By contrast, if optarg3 is specified then optarg1 & optarg2 are not used. If it were one optional argument it'd be easy for the function to "know" which argument to use: if optarg1 != None: do something else: do something else My question is how to I "tell" the function which optional argument to use when there's multiple optional arguments and not all of them are