www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - The future of UDAs.

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--f46d04016ab76ef67f04cf7530a2
Content-Type: text/plain; charset=UTF-8

Hi, fellow D programmers.

I'd like to know (and I think I'm not alone with this) the future plans
about UDAs, the features they're planned to have and the features they're
planned not to have.
I have a curious project, which would be vastly easier and cleaner with
proper UDAs, so naturally I'd like to know what to expect, because if what
I need is planned to be available, I'll postpone my project, instead of
rushing into an ugly solution.

AFAIK, currently UDAs are set at declaration site, are immutable and the
declaration cannot get additional UDAs externally. Moreover, only global
declarations can have UDAs, which removes some very useful possible uses of
UDAs.

Lack of mutable compile-time variables sometimes cripples the
metaprogramming in D. For instance all classes, derived from a certain type
must be dealt with in a special way, which requires a tuple of those types.
Gathering a tuple of unrelated types is currently impossible, because that
would require mutable compile-time variables.

Mutable compile-time variables would also be extremely useful for
implementing a very powerful compile-time reflection library without the
need for compiler magic. All you'd have to do is mix in a template in your
class or your module and voila. The mixin would then add the introspected
declarations to the central compile-time declaration repository.

There are also many cases when some actions need to happen in case a
declaration gets a UDA of a specific type. For instance, a powerful RTTI
library, which (when a type gets a dedicated RTTI-typed UDA) adds the
run-time information about the class to the central registry at load-time.
This kind of stuff could be easily achieved using the constructors and
destructors of the structures, being added to the UDAs of a declaration (in
this case a class). The only missing thing for this to work is the ability
for the constructor to see the declaration it's being put on. I'd
personally expect there to be some sort of a
__traits(getAttributeDeclaration), which would evaluate to the symbol on
which the enclosing type (a structure, a union or whatever) is being placed
on as an attribute.

The point is, that with a tiny little boost, the UDAs could make D's
meta-programming a tool so powerful, it would be very difficult to predict
the limit of possibilities..

Note, that this thread isn't about the syntax, but about the expected and
planned functionality of UDAs and their use cases.

Please share your thoughts about this.

-- 
Bye,
Gor Gyolchanyan.

--f46d04016ab76ef67f04cf7530a2
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hi, fellow D programmers.<div><br></div><div>I&#39;d like to know (and I th=
ink I&#39;m not alone with this) the future plans about UDAs, the features =
they&#39;re planned to have and the features they&#39;re planned not to hav=
e.</div>
<div>I have a curious project, which would be vastly easier and cleaner wit=
h proper UDAs, so naturally I&#39;d like to know what to expect, because if=
 what I need is planned to be available, I&#39;ll postpone my project, inst=
ead of rushing into an ugly solution.</div>
<div><br></div><div>AFAIK, currently UDAs are set at declaration site, are =
immutable and the declaration cannot get additional UDAs externally. Moreov=
er, only global declarations can have UDAs, which removes some very useful =
possible uses of UDAs.</div>
<div><br></div><div>Lack of mutable compile-time variables sometimes crippl=
es the metaprogramming in D. For instance all classes, derived from a certa=
in type must be dealt with in a special way, which requires a tuple of thos=
e types. Gathering a tuple of unrelated types is currently impossible, beca=
use that would require mutable compile-time variables.</div>
<div><br></div><div>Mutable compile-time variables would also be extremely =
useful for implementing a very powerful compile-time reflection library wit=
hout the need for compiler magic. All you&#39;d have to do is mix in a temp=
late in your class or your module and voila. The mixin would then add the i=
ntrospected declarations to the central compile-time declaration repository=
.</div>
<div><br></div><div>There are also many cases when some actions need to hap=
pen in case a declaration gets a UDA of a specific type. For instance, a po=
werful RTTI library, which (when a type gets a dedicated RTTI-typed UDA) ad=
ds the run-time information about the class to the central registry at load=
-time. This kind of stuff could be easily achieved using the constructors a=
nd destructors of the structures, being added to the UDAs of a declaration =
(in this case a class). The only missing thing for this to work is the abil=
ity for the constructor to see the declaration it&#39;s being put on. I&#39=
;d personally expect there to be some sort of a __traits(getAttributeDeclar=
ation), which would evaluate to the symbol on which the enclosing type (a s=
tructure, a union or whatever) is being placed on as an attribute.=C2=A0</d=
iv>
<div><br></div><div>The point is, that with a tiny little boost, the UDAs c=
ould make D&#39;s meta-programming a tool so powerful, it would be very dif=
ficult to predict the limit of possibilities..</div><div><br></div><div>
Note, that this thread isn&#39;t about the syntax, but about the expected a=
nd planned functionality of UDAs and their use cases.</div><div><br></div><=
div><div>Please share your thoughts about this.</div><div><br></div>-- <br>
Bye,<br>Gor Gyolchanyan.<br>
</div>

--f46d04016ab76ef67f04cf7530a2--
Nov 26 2012
next sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 11/27/2012 6:42 PM, Gor Gyolchanyan wrote:
 AFAIK, currently UDAs are set at declaration site, are immutable and the
 declaration cannot get additional UDAs externally. Moreover, only global
 declarations can have UDAs, which removes some very useful possible uses
 of UDAs.

What very useful thing could be done with UDAs on local variables?
 Lack of mutable compile-time variables sometimes cripples the
 metaprogramming in D. For instance all classes, derived from a certain
 type must be dealt with in a special way, which requires a tuple of
 those types. Gathering a tuple of unrelated types is currently
 impossible, because that would require mutable compile-time variables.

I don't understand this. It is perfectly possible to create compile time tuple of (int, long, float, int*, S).
 Mutable compile-time variables would also be extremely useful for
 implementing a very powerful compile-time reflection library without the
 need for compiler magic. All you'd have to do is mix in a template in
 your class or your module and voila. The mixin would then add the
 introspected declarations to the central compile-time declaration
 repository.

I don't understand the difficulty here. UDAs are additive, so you could mixin your template in one space at the top, and it will apply to all the declarations nested under it, exactly like other attributes do now.
 There are also many cases when some actions need to happen in case a
 declaration gets a UDA of a specific type. For instance, a powerful RTTI
 library, which (when a type gets a dedicated RTTI-typed UDA) adds the
 run-time information about the class to the central registry at
 load-time.

That can be done now. Loop over the declarations, checking to see if each has a particular attribute.
 This kind of stuff could be easily achieved using the
 constructors and destructors of the structures, being added to the UDAs
 of a declaration (in this case a class). The only missing thing for this
 to work is the ability for the constructor to see the declaration it's
 being put on. I'd personally expect there to be some sort of a
 __traits(getAttributeDeclaration), which would evaluate to the symbol on
 which the enclosing type (a structure, a union or whatever) is being
 placed on as an attribute.

That could cause ugly forward reference issues, and also recall that a UDA can be applied to many declarations at once.
Nov 27 2012
parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 11/27/2012 10:27 PM, Gor Gyolchanyan wrote:
 I think I really don't know what exactly do the UDAs do at the moment,
 judging from your reply.The only documentation was the original
 D.anounce post of yours. Is there a way I can get a reliable
 documentation on this?

 Use cases:

 1. I have a declaration with a UDA struct. I introspect the types I need
 and add their names to an array in that struct. read that struct in a
 static constructor and do necessary stuff with all those types. Is this
 possible currently?

Yes.
 2. I have some code, which must be run on any type, marked with a
 special UDA. Can I have that code executed without explicitly having to
 mix stuff in every module?

It'll either have to be explicitly mixed in with every module that uses the UDA or some other code you write will explicitly need to apply it to each module that uses it. I suggest the former would be more convenient. Note that if a module is to use a UDA, you'll have to edit it anyway, so it shouldn't be a big issue to add the mixin.
Nov 27 2012
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 11/28/2012 07:38 AM, Gor Gyolchanyan wrote:
 So you're saying that UDAs are mutable after all? You can change a UDA
 but you can't add a new one, right? If that's the case, it will solve
 ALL my problems. I'll have all types marked with my struct and BAM! I
 have mutable compile-time variables, which I solely needed.
 ...

There is no such thing as a 'mutable compile-time variable'. How would such a thing behave? The term is meaningless until this is explained.
Nov 28 2012
prev sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 11/28/2012 5:38 PM, Gor Gyolchanyan wrote:
 So you're saying that UDAs are mutable after all? You can change a UDA
 but you can't add a new one, right? If that's the case, it will solve
 ALL my problems. I'll have all types marked with my struct and BAM! I
 have mutable compile-time variables, which I solely needed.

I don't know what you mean. The static constructor thing happens at runtime.
Nov 28 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--f46d0420a695c0e1be04cf785472
Content-Type: text/plain; charset=UTF-8

I think I really don't know what exactly do the UDAs do at the moment,
judging from your reply.The only documentation was the original D.anounce
post of yours. Is there a way I can get a reliable documentation on this?

Use cases:

1. I have a declaration with a UDA struct. I introspect the types I need
and add their names to an array in that struct. read that struct in a
static constructor and do necessary stuff with all those types. Is this
possible currently?

2. I have some code, which must be run on any type, marked with a special
UDA. Can I have that code executed without explicitly having to mix stuff
in every module?

I guess the way UDAs were originally designed, they have absolutely nothing
to do with what I envisioned.




On Tue, Nov 27, 2012 at 3:11 PM, Walter Bright
<newshound2 digitalmars.com>wrote:

 On 11/27/2012 6:42 PM, Gor Gyolchanyan wrote:

 AFAIK, currently UDAs are set at declaration site, are immutable and the
 declaration cannot get additional UDAs externally. Moreover, only global
 declarations can have UDAs, which removes some very useful possible uses
 of UDAs.

What very useful thing could be done with UDAs on local variables? Lack of mutable compile-time variables sometimes cripples the
 metaprogramming in D. For instance all classes, derived from a certain
 type must be dealt with in a special way, which requires a tuple of
 those types. Gathering a tuple of unrelated types is currently
 impossible, because that would require mutable compile-time variables.

I don't understand this. It is perfectly possible to create compile time tuple of (int, long, float, int*, S). Mutable compile-time variables would also be extremely useful for
 implementing a very powerful compile-time reflection library without the
 need for compiler magic. All you'd have to do is mix in a template in
 your class or your module and voila. The mixin would then add the
 introspected declarations to the central compile-time declaration
 repository.

I don't understand the difficulty here. UDAs are additive, so you could mixin your template in one space at the top, and it will apply to all the declarations nested under it, exactly like other attributes do now. There are also many cases when some actions need to happen in case a
 declaration gets a UDA of a specific type. For instance, a powerful RTTI
 library, which (when a type gets a dedicated RTTI-typed UDA) adds the
 run-time information about the class to the central registry at
 load-time.

That can be done now. Loop over the declarations, checking to see if each has a particular attribute. This kind of stuff could be easily achieved using the
 constructors and destructors of the structures, being added to the UDAs
 of a declaration (in this case a class). The only missing thing for this
 to work is the ability for the constructor to see the declaration it's
 being put on. I'd personally expect there to be some sort of a
 __traits(**getAttributeDeclaration), which would evaluate to the symbol
 on
 which the enclosing type (a structure, a union or whatever) is being
 placed on as an attribute.

That could cause ugly forward reference issues, and also recall that a UDA can be applied to many declarations at once.

-- Bye, Gor Gyolchanyan. --f46d0420a695c0e1be04cf785472 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable I think I really don&#39;t know what exactly do the UDAs do at the moment, = judging from your reply.The only documentation was the original D.anounce p= ost of yours. Is there a way I can get a reliable documentation on this?=C2= =A0<div> <br></div><div>Use cases:</div><div><br></div><div>1. I have a declaration = with a UDA struct. I introspect the types I need and add their names to an = array in that struct. read that struct in a static constructor and do neces= sary stuff with all those types.=C2=A0Is this possible currently?</div> <div><br></div><div>2. I have some code, which must be run on any type, mar= ked with a special UDA. Can I have that code executed without explicitly ha= ving to mix stuff in every module?</div><div><br></div><div>I guess the way= UDAs were originally designed, they have absolutely nothing to do with wha= t I envisioned.</div> <div><br></div><div><br></div><div class=3D"gmail_extra"><br><br><div class= =3D"gmail_quote">On Tue, Nov 27, 2012 at 3:11 PM, Walter Bright <span dir= =3D"ltr">&lt;<a href=3D"mailto:newshound2 digitalmars.com" target=3D"_blank= ">newshound2 digitalmars.com</a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"><div class=3D"im">On 11/27/2012 6:42 PM, Gor= Gyolchanyan wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> AFAIK, currently UDAs are set at declaration site, are immutable and the<br=



</blockquote> <br></div> What very useful thing could be done with UDAs on local variables?<div clas= s=3D"im"><br> <br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Lack of mutable compile-time variables sometimes cripples the<br> metaprogramming in D. For instance all classes, derived from a certain<br> type must be dealt with in a special way, which requires a tuple of<br> those types. Gathering a tuple of unrelated types is currently<br> impossible, because that would require mutable compile-time variables.<br> </blockquote> <br></div> I don&#39;t understand this. It is perfectly possible to create compile tim= e tuple of (int, long, float, int*, S).<div class=3D"im"><br> <br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Mutable compile-time variables would also be extremely useful for<br> implementing a very powerful compile-time reflection library without the<br=

r> your class or your module and voila. The mixin would then add the<br> introspected declarations to the central compile-time declaration<br> repository.<br> </blockquote> <br></div> I don&#39;t understand the difficulty here. UDAs are additive, so you could= mixin your template in one space at the top, and it will apply to all the = declarations nested under it, exactly like other attributes do now.<div cla= ss=3D"im"> <br> <br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> There are also many cases when some actions need to happen in case a<br> declaration gets a UDA of a specific type. For instance, a powerful RTTI<br=

run-time information about the class to the central registry at<br> load-time.<br> </blockquote> <br></div> That can be done now. Loop over the declarations, checking to see if each h= as a particular attribute.<div class=3D"im"><br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> This kind of stuff could be easily achieved using the<br> constructors and destructors of the structures, being added to the UDAs<br> of a declaration (in this case a class). The only missing thing for this<br=

br> being put on. I&#39;d personally expect there to be some sort of a<br> __traits(<u></u>getAttributeDeclaration), which would evaluate to the symbo= l on<br> which the enclosing type (a structure, a union or whatever) is being<br> placed on as an attribute.<br> </blockquote> <br></div> That could cause ugly forward reference issues, and also recall that a UDA = can be applied to many declarations at once.<br> <br> </blockquote></div><br><br clear=3D"all"><div><br></div>-- <br>Bye,<br>Gor = Gyolchanyan.<br> </div> --f46d0420a695c0e1be04cf785472--
Nov 27 2012
prev sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--14dae9d717bcff0b2e04cf886927
Content-Type: text/plain; charset=UTF-8

So you're saying that UDAs are mutable after all? You can change a UDA but
you can't add a new one, right? If that's the case, it will solve ALL my
problems. I'll have all types marked with my struct and BAM! I have mutable
compile-time variables, which I solely needed.


On Wed, Nov 28, 2012 at 1:10 AM, Walter Bright
<newshound2 digitalmars.com>wrote:

 On 11/27/2012 10:27 PM, Gor Gyolchanyan wrote:

 I think I really don't know what exactly do the UDAs do at the moment,
 judging from your reply.The only documentation was the original
 D.anounce post of yours. Is there a way I can get a reliable
 documentation on this?

 Use cases:

 1. I have a declaration with a UDA struct. I introspect the types I need
 and add their names to an array in that struct. read that struct in a
 static constructor and do necessary stuff with all those types. Is this
 possible currently?

Yes. 2. I have some code, which must be run on any type, marked with a
 special UDA. Can I have that code executed without explicitly having to
 mix stuff in every module?

It'll either have to be explicitly mixed in with every module that uses the UDA or some other code you write will explicitly need to apply it to each module that uses it. I suggest the former would be more convenient. Note that if a module is to use a UDA, you'll have to edit it anyway, so it shouldn't be a big issue to add the mixin.

-- Bye, Gor Gyolchanyan. --14dae9d717bcff0b2e04cf886927 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable So you&#39;re saying that UDAs are mutable after all? You can change a UDA = but you can&#39;t add a new one, right? If that&#39;s the case, it will sol= ve ALL my problems. I&#39;ll have all types marked with my struct and BAM! = I have mutable compile-time variables, which I solely needed.<div class=3D"= gmail_extra"> <br><br><div class=3D"gmail_quote">On Wed, Nov 28, 2012 at 1:10 AM, Walter = Bright <span dir=3D"ltr">&lt;<a href=3D"mailto:newshound2 digitalmars.com" = target=3D"_blank">newshound2 digitalmars.com</a>&gt;</span> wrote:<br><bloc= kquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #cc= c solid;padding-left:1ex"> <div class=3D"im">On 11/27/2012 10:27 PM, Gor Gyolchanyan wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> I think I really don&#39;t know what exactly do the UDAs do at the moment,<= br> judging from your reply.The only documentation was the original<br> D.anounce post of yours. Is there a way I can get a reliable<br> documentation on this?<br> <br> Use cases:<br> <br> 1. I have a declaration with a UDA struct. I introspect the types I need<br=

static constructor and do necessary stuff with all those types. Is this<br> possible currently?<br> </blockquote> <br></div> Yes.<div class=3D"im"><br> <br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> 2. I have some code, which must be run on any type, marked with a<br> special UDA. Can I have that code executed without explicitly having to<br> mix stuff in every module?<br> </blockquote> <br></div> It&#39;ll either have to be explicitly mixed in with every module that uses= the UDA or some other code you write will explicitly need to apply it to e= ach module that uses it. I suggest the former would be more convenient. Not= e that if a module is to use a UDA, you&#39;ll have to edit it anyway, so i= t shouldn&#39;t be a big issue to add the mixin.<br> <br> </blockquote></div><br><br clear=3D"all"><div><br></div>-- <br>Bye,<br>Gor = Gyolchanyan.<br> </div> --14dae9d717bcff0b2e04cf886927--
Nov 27 2012