default-parameters

Can an unnamed parameter of function have a default value?

眉间皱痕 提交于 2019-12-04 01:33:04
Is the following code legal in C++? void f(void* = 0) {} int main() { f(); } Which page of the C++ standard states that this usage is legal? Yes, it's legal. There is no standard wording to allow this combination of features specifically; there simply isn't any to disallow it, either. Default argument syntax applies to function parameters in a parameter-declaration : [C++11: 8.3.6/1]: If an initializer-clause is specified in a parameter-declaration this initializer-clause is used as a default argument. Default arguments will be used in calls where trailing arguments are missing. ...and

Is there a way to use the default value on a non-optional parameter when null is passed?

拜拜、爱过 提交于 2019-12-03 09:26:40
For example, if I have the following data class: data class Data( val name: String = "", val number: Long = 0 ) And functions that can return null : fun newName(): String? {} fun newNumber(): Long? {} I know I can use the following to use the value of the functions if they are not null : val newName = newName() val newNumber = newNumber() val data = Data( if (newName != null) newName else "", if (newNumber != null) newNumber else 0 ) But is there a way to just use the default value specified in the constructor of the Data class when the values are null ? I could not find anything in the

Accessing a function's default parameter value in Kotlin

ぐ巨炮叔叔 提交于 2019-12-01 21:12:21
问题 Can a function's default parameter value be accessed from a function extension, or anywhere else? fun DieRoll.cheatRoll():Int = roll(min = max -1) fun roll(min: Int = 1, max: Int = 6): Int = (min..max).rand() 回答1: No, this is not possible. The default values are not accessible. They are just contained in a bridge-method in the bytecode: fun test(a: Int = 123) { } fun test2() { test() test(100) } Results in the bytecode: public final test(int arg0) { //(I)V <localVar:index=0 , name=this , desc

function call with default parameter

北城余情 提交于 2019-12-01 20:12:32
I wrote an examination about C++ programming. There was one question where I and my professor didn't agree. The question was, does the following function work or not: #include <iostream> using namespace std; void f(int=4, long=10, double=3.14); int main( int argc , char ** argv ) { f( , ,8); return EXIT_SUCCESS; } void f(int i, long l, double d) { cout << i << " " << " " << l << " " << d; } I said it would not work, but my professor said it will definitely work because of the default parameter in the function declaration. I tried it with MSVC and it didn't work. Is that compiler-specific? How

Accessing a function's default parameter value in Kotlin

浪尽此生 提交于 2019-12-01 19:25:05
Can a function's default parameter value be accessed from a function extension, or anywhere else? fun DieRoll.cheatRoll():Int = roll(min = max -1) fun roll(min: Int = 1, max: Int = 6): Int = (min..max).rand() guenhter No, this is not possible. The default values are not accessible. They are just contained in a bridge-method in the bytecode: fun test(a: Int = 123) { } fun test2() { test() test(100) } Results in the bytecode: public final test(int arg0) { //(I)V <localVar:index=0 , name=this , desc=Lorg/guenhter/springboot/kt/Fun;, sig=null, start=L1, end=L2> <localVar:index=1 , name=a , desc=I,

How can I use a static method as a default parameter for the strategy design pattern?

白昼怎懂夜的黑 提交于 2019-12-01 17:34:56
I want to make a class that uses a strategy design pattern similar to this: class C: @staticmethod def default_concrete_strategy(): print("default") @staticmethod def other_concrete_strategy(): print("other") def __init__(self, strategy=C.default_concrete_strategy): self.strategy = strategy def execute(self): self.strategy() This gives the error: NameError: name 'C' is not defined Replacing strategy=C.default_concrete_strategy with strategy=default_concrete_strategy will work but, left as default, the strategy instance variable will be a static method object rather than a callable method.

Why C++ does not allow function parameters used for default values latter parameters?

谁说胖子不能爱 提交于 2019-12-01 16:44:36
This is a follow-up on this question . The code in the OP question there looked quite reasonable and unambiguous to me. Why does not C++ allow using former parameters to define default values of latter parameters, something like this: int foo( int a, int b = a ); Also, at least in C++11 declared types of parameters can be used to determine the return type, so it's not unheard of to use function parameters in similar manner: auto bar( int a ) -> decltype( a ); Thus the question: what are the reason(s) why the above declaration of foo is not allowed? For one thing, this would require that a is

How to find out the default values of a particular function's argument in another function in Python?

安稳与你 提交于 2019-12-01 16:10:15
Let's suppose we have a function like this: def myFunction(arg1='a default value'): pass We can use introspection to find out the names of the arguments that myFunction() takes using myFunction.func_code.co_varnames , but how to find out the default value of arg1 (which is 'a default value' in the above example)? As an alternative to rooting around in the attributes of the function you can use the inspect module for a slightly friendlier interface: For Python 3.x interpreters: import inspect spec = inspect.getfullargspec(myFunction) Then spec is a FullArgSpec object with attributes such as

Why C++ does not allow function parameters used for default values latter parameters?

天大地大妈咪最大 提交于 2019-12-01 16:00:38
问题 This is a follow-up on this question. The code in the OP question there looked quite reasonable and unambiguous to me. Why does not C++ allow using former parameters to define default values of latter parameters, something like this: int foo( int a, int b = a ); Also, at least in C++11 declared types of parameters can be used to determine the return type, so it's not unheard of to use function parameters in similar manner: auto bar( int a ) -> decltype( a ); Thus the question: what are the

PhpStorm - JavaScript function parameter IDE error

天大地大妈咪最大 提交于 2019-12-01 15:47:19
I am trying to build a reusable JavaScript function that makes use of default parameters. However, the IDE warns me I do something wrong. The code is this: function standardAjaxRequest(process_script_path, redirect = false) { var loading = $(".loading"); var response_message = $(".response_message"); // runs the ajax to add the entry submitted form.on("submit", function(event) { event.preventDefault(); removeNoticeError(); var data = form.serialize(); $.ajax({ url: process_script_path, type: "POST", dataType: "json", data: data, cache: false, success: function(response) { if(response.status ==