3 ms·
In this: [CallerArgumentExpression("condition")] string expression = "" what is the role of the `= ""`? I think the answer is given by: If null or a str
by ridiculous_fish 5y ago
In this:
[CallerArgumentExpression("condition")] string expression = ""
what is the role of the `= ""`?
I think the answer is given by:
If null or a string that is not a parameter name (e.g."notAParameterName") is provided, the compiler will pass in an empty string
so if you mistype the parameter name in the decoration, there's no warning, it just gives you an empty string? Why allow that?
- gregoryl 5y agoIt makes it an optional parameter, which means you don't need to pass it. Otherwise every call would need to send an empty string or similar.
- ridiculous_fish 5y agoBut this parameter is supposed to be auto-populated with the text from another parameter.
- deleted 5y ago[deleted]
- wvenable 5y agoIt's only populated if the parameter isn't provided explicitly.
- kevingadd 5y agoYou can pass it manually, and you can invoke this method from other compilers that don't implement the attribute. In those cases you want an explicit parameter and one with a default value.
- ridiculous_fish 5y agoThis explanation makes sense to me. If you invoke this function with reflection, there's no hope of capturing the textual argument from other parameters.
- pryce 5y agoI believe the `= ""` makes this parameter 1. have a default value of empty string and 2. more importantly, makes it fit with the existing syntax for a function signature that indicates the function argument is optional. If I am not mistaken, that means that the language feature can be included as simply as the newly supported 'CallerArgumentExpression' annotation without needing any change to the language specification for what defines a the correct syntax of a function signature.
- deleted 5y ago[deleted]
- ripley12 5y ago> so if you mistype the parameter name in the decoration, there's no warning Mistyping the parameter name results in the following compiler warning, thankfully: CS8963 The CallerArgumentExpressionAttribute applied to parameter 'conditionExpression' will have no effect. It is applied with an invalid parameter name.
- tialaramex 5y agoNice, an actual error that describes what you did wrong and why it's wrong. That's what you want to see in the twenty first century. Does it have typo checks too, so that if you wrote coditionExpression or conditionExpresion it would point out that maybe you meant the parameter conditionExpression ?