Questions tagged [named-parameters]

Named parameters enable you to specify an argument for a particular parameter by associating the argument with the parameter's name rather than with the parameter's position in the parameter list.

371 questions
35
votes
4 answers

No named parameters in Ruby?

This is so simple that I can't believe it caught me. def meth(id, options = "options", scope = "scope") puts options end meth(1, scope = "meh") -> "meh" I tend to use hashes for argument options just because it was how the herd did it– and it…
JohnMetta
  • 18,782
  • 5
  • 31
  • 57
35
votes
3 answers

Passing lambda functions as named parameters in C#

Compile this simple program: class Program { static void Foo( Action bar ) { bar(); } static void Main( string[] args ) { Foo( () => Console.WriteLine( "42" ) ); } } Nothing strange there. If we make an…
Danko Durbić
  • 7,077
  • 5
  • 34
  • 39
35
votes
12 answers

Named Parameters in Ruby Structs

I'm pretty new to Ruby so apologies if this is an obvious question. I'd like to use named parameters when instantiating a Struct, i.e. be able to specify which items in the Struct get what values, and default the rest to nil. For example I want to…
Matt S.
  • 9,902
  • 6
  • 29
  • 25
35
votes
6 answers

C++ "Named Parameter Idiom" vs. Boost::Parameter library

I've looked at both the Named Parameter Idiom and the Boost::Parameter library. What advantages does each one have over the other? Is there a good reason to always choose one over the other, or might each of them be better than the other in some…
Head Geek
  • 38,128
  • 22
  • 77
  • 87
33
votes
6 answers

I want to use named parameters in Dart for clarity. How should I handle them?

TL;DR: Named parameters are optional as a result of a conscious design choice. Short of having official language support, is there any way to enforce (and inform) required named arguments? I find it extremely useful to use named parameters when…
Kafeaulait
  • 664
  • 1
  • 6
  • 14
32
votes
1 answer

How can I force calls to some constructors/functions to use named arguments?

I have some constructors and functions that I'd like to always be called with named arguments. Is there a way to require this? I'd like to be able to do this for constructors and functions with many parameters and for those that read more clearly…
mfulton26
  • 29,956
  • 6
  • 64
  • 88
31
votes
2 answers

what is the difference between required and @required in flutter. What is the difference between them and when do we need to use them?

If I delete required from the named parameters, it gives me an error: The parameter 'color' // can't have a value of 'null' because of its type, but the implicit default value is 'null'. What is the difference between them and when do we need to…
ulukbek
  • 483
  • 4
  • 10
28
votes
1 answer

named parameters with default values in groovy

Is it possible to have named parameters with default values in groovy? My plan is to make a sort of object factory, which can be called with no arguments at all in order to get an object with default values. Also, I'd need the functionality to…
kaskelotti
  • 4,709
  • 9
  • 45
  • 72
26
votes
10 answers

vsprintf or sprintf with named arguments, or simple template parsing in PHP

I'm searching for a way to use named arguments for sprintf or printf. Example: sprintf( 'Last time logged in was %hours hours, %minutes minutes, %seconds seconds ago' ,$hours,$minutes, $seconds ); or via vsprintf and an associative array. I…
Jeremy S.
  • 6,423
  • 13
  • 48
  • 67
26
votes
6 answers

How to call a function with a dictionary that contains more items than the function has parameters?

I am looking for the best way to combine a function with a dictionary that contains more items than the function's inputs basic **kwarg unpacking fails in this case: def foo(a,b): return a + b d = {'a':1, 'b':2, 'c':3} foo(**d) -->…
25
votes
1 answer

How can I pass named arguments to a Rake task?

Is there a way to pass named arguments to a Rake task without using environment variables? I am aware that Rake tasks can accept arguments in two formats: Environment Variables $ rake my_task foo=bar This creates an environment variable with the…
Andrew
  • 1,590
  • 4
  • 19
  • 25
24
votes
2 answers

Using named parameters in node.js

I am using node.js v4.3.1 I would like to use named parameters in calling functions as they are more readable. In python, I can call a function in this manner; info(spacing=15, width=46) How do I do the same in node.js? My javascript function…
guagay_wk
  • 26,337
  • 54
  • 186
  • 295
24
votes
2 answers

Hibernate could not locate named parameter even if it exist

Hibernate Keeps detecting org.hibernate.QueryParameterException: could not locate named parameter [name] even though it exist. here's my hql Query query = sess().createQuery("from UserProfile where firstName LIKE '%:name%'").setParameter("name",…
user962206
  • 15,637
  • 61
  • 177
  • 270
23
votes
3 answers

Why do we need to specify parameter name in interface?

When we create interface methods, can't we do something like in java: void interface_method(Integer,String, /* other parameter */); Instead I noticed that we also need to give the parameter names lile: void interface_method(Integer i, String…
Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82
22
votes
5 answers

Why does Swift require parameter names if it also requires argument order?

In Swift, when you call a function, you are required to label the arguments, unless the author of the function has explicitly allow you not to. Is there a reason for this in terms of language design? I had always viewed parameter labels as a good…
Alex N.
  • 654
  • 8
  • 21
1
2
3
24 25