www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Attributes on parameters in functions.

reply TheFlyingFiddle <borin.lukas gmail.com> writes:


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.
Oct 30 2015
next sibling parent reply Meta <jared771 gmail.com> writes:
On Friday, 30 October 2015 at 21:28:09 UTC, TheFlyingFiddle wrote:


 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.
Oct 30 2015
parent Timon Gehr <timon.gehr gmx.ch> writes:
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
Oct 30 2015
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2015-10-30 22:28, TheFlyingFiddle wrote:


 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
Oct 31 2015
parent TheFlyingFiddle <borin.lukas gmail.com> writes:
On Saturday, 31 October 2015 at 12:45:13 UTC, Jacob Carlborg 
wrote:
 On 2015-10-30 22:28, TheFlyingFiddle wrote:


 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.
Oct 31 2015