Thread overview
Attributes on parameters in functions.
Oct 30, 2015
TheFlyingFiddle
Oct 31, 2015
Meta
Oct 31, 2015
Timon Gehr
Oct 31, 2015
Jacob Carlborg
Oct 31, 2015
TheFlyingFiddle
October 30, 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.

October 31, 2015
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.
October 31, 2015
On 10/31/2015 02:02 AM, Meta wrote:
> On Friday, 30 October 2015 at 21:28:09 UTC, TheFlyingFiddle wrote:
>> ...
>>
>> 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.

http://forum.dlang.org/thread/k7afq6$2832$1@digitalmars.com?page=14
October 31, 2015
On 2015-10-30 22:28, 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:

https://github.com/D-Programming-Language/dmd/pull/4783

-- 
/Jacob Carlborg
October 31, 2015
On Saturday, 31 October 2015 at 12:45:13 UTC, Jacob Carlborg wrote:
> On 2015-10-30 22:28, 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:
>
> https://github.com/D-Programming-Language/dmd/pull/4783

This looks very promising I hope something like this get's pulled eventually.