[Proposal]: Allow specifying default values for struct method arguments #4378
Unanswered
aaronfranke
asked this question in
Language Ideas
Replies: 2 comments
-
There has been discussion previously about blittable defaults, but I don't think that went anywhere. |
Beta Was this translation helpful? Give feedback.
0 replies
-
I think the chief issue here is how this would be implemented. The current status is that default parameters encode the default value in an attribute. Attributes can only store a small set of data. Thus it's not obvious how this would be implemented. If the CLR expanded the set of things which can be stored in attributes, my guess is that the LDM would very likely expand the set of things that can be used as default arguments. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Default values for structs in method arguments
Summary
I would like to provide default arguments for
struct
s in my methods. However, C# does not support this feature, currently only types that can be declaredconst
are supported as default values for method arguments, andstruct
s cannot beconst
.Motivation
I would like to write code that looks like this:
However. C# does not support
struct
s as arguments with default values. Currently I can use this as a work-around:Or this would also work (possibly faster because it doesn't involve nullability...?):
Or a third work-around would be to just always specify the arguments when calling, but this isn't ideal.
Detailed design
The design of this proposal does not have much more detail than already stated. The feature would allow the first code example to compile, and have the same behavior as the two work-arounds (except likely with better performance).
One concern is with the reason that
struct
s can't be declaredconst
in C# which is that astruct
constructor is evaluated at runtime and can have potentially complex logic in it. However, in the use cases I'm thinking of, this is not the case, the constructors I'm using are very simple and only serve to populate the fields. Depending on how this is implemented, we would likely need to guarantee that the struct constructor doesn't do anything fancy, or we can provide some other way to populate the fields of the struct (or if you just give me a way to specify the raw bytes that would work too I guess). Or, even better, would it be possible to allow default method arguments that aren'tconst
values, such that when callingClamp()
it would act just like if you included the arguments likeClamp(new Color(0, 0, 0, 0), new Color(1, 1, 1, 1))
?Drawbacks
There aren't any drawbacks that I'm aware of aside from the concern above. People not using this feature wouldn't be affected.
Alternatives
See the work-arounds in the "Motivation" section and the ideas for how to fix the above concern.
Unresolved questions
There is the question of what do about the above concern.
Design meetings
None so far.
Beta Was this translation helpful? Give feedback.
All reactions