Language Proposal: params dictionary #9151
Replies: 4 comments 6 replies
-
I'm not understanding why you can't just use a regular signature. You have an expected order, and expected types. Seems like normal optional parameters would be just fine. |
Beta Was this translation helpful? Give feedback.
-
Why can't you use optional parameters? public torch.Tensor forward(
torch.autograd.AutogradContext ctx,
torch.Tensor a,
torch.Tensor b,
torch.Tensor? output = null,
torch.Tensor? bias = null,
F.QuantState? quant_state = null) forward(ctx, a, b, bias: bias); |
Beta Was this translation helpful? Give feedback.
-
Can you explain where it is better than creating dictionary ?
|
Beta Was this translation helpful? Give feedback.
-
You will be able to write this as the following in C#14:
This is succinct and clear. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
params dictionary
Motivation
While working with TorchSharp, I found I have to use
params object[] vars
to support variadic arguments as input. For example:The C# way has several issues:
While in Python you can just write it like:
Apparently
params collection
is really hard to work with in AL/ML scenarios, but we needparams
here so that user can pass variadic arguments to the method.Proposal
To address this, I proposed C# to support
params dictionary
, likeparams Dictionary<string, T>
,params ConcurrentDictionary<string, T>
andparams IDictionary<string, T>
..., where the key stands for the argument name, and the value is the value of argument. And this also perfectly falls into the category ofparams collection
.With
params dictionary
, the above code will become:This eliminates the necessity of inserting
null
just to keeping the ordinal of arguments, and allows you to pass arguments in a optional nominal arguments way.Alternative
Dictionary with dictionary collection expression
It will still be better to have
params dictionary
as you don't have to type the redundant[ ]
or the quote symbols around the keyParams tuple
We use a
params (string Key, T Value)[] args
, which is the existing concept in today's C#, but we can allow omitting the braces and using like named arguments at call sites:Beta Was this translation helpful? Give feedback.
All reactions