Attributes on parameters in functions.

TheFlyingFiddle via Digitalmars-d digitalmars-d at puremagic.com
Fri Oct 30 14:28:08 PDT 2015


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.



More information about the Digitalmars-d mailing list