On Friday, 30 October 2015 at 21:28:09 UTC, TheFlyingFiddle wrote:
In other languages that have Attributes (Java and C# atleast)
I can do stuff like this: (Java)
//com.bar.java
interface Bar { /*stuff*/ }
//com.foo.java
class Foo
{
Foo(@Bar int a)
{
//some stuff
}
}
I don't seem to be able to do this in D. That is I cannot do
this:
enum Bar;
void foo(@Bar int a) { /* stuff */ }
//Error: basic type expected, not @
Is there a reason that attributes cannot be used on parameters
in functions?
My current use-case for this is that I would like to use UDA's
for pattern matching.
auto value = Tuple!(int, "x", int, "y")(1,1);
value.match!(
(@(0,1) int a, @(0,1) int b) => ...,
(@isPrime int a, int b) => ...,
(@(x => (x % 2)) int a, int b) => ...,
(@Bind!"y" int a) => ...);
Basically match to the first lambda if "x" == 0 | 1 and "y" ==
0 | 1, to the second if a "x" is prime third if "x" is odd,
else explicitly bind "y" to a.
I know I can technically do. Match! or M! instead of the @
symbol to achieve the same effect which is probably what I will
end up doing but it would be nice if we could use attributes in
function parameters.
Some people wanted it when UDAs were first added to D but Walter
didn't see any reason to so he didn't.