January 05, 2013 Re: So, User-Defined Attributes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright Attachments:
| On Sat, Jan 5, 2013 at 11:14 PM, Walter Bright <newshound2@digitalmars.com>wrote: > On 1/5/2013 2:06 PM, Philippe Sigaud wrote: > >> But why is @(MyType) accepted, whereas @(int) is not? >> > > Because it's looking for an expression inside the parents, and int is not an expression. > > Well, first that would be nice to have the grammar online :) Even if I understand the grammar restriction, from a user PoV this is quite unexpected: a built-in type is a completely legal part of a D tuple. Either it should be restricted to user-defined types (something I don't like), or, if attributes are supposed to be tuples, then any tuple element should be authorized. And I will not buy a possible argument saying that I can wrap an int inside a user-defined type. Yes, I can. But then, there is the same solution for @(3) or @("Hello") and *these* are legal. |
January 05, 2013 Re: So, User-Defined Attributes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | On 2013-01-05 23:44, Philippe Sigaud wrote: > Well, first that would be nice to have the grammar online :) Yeah, still waiting for that pull request to be merged. -- /Jacob Carlborg |
January 06, 2013 Re: So, User-Defined Attributes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Saturday, 5 January 2013 at 22:57:03 UTC, Jacob Carlborg wrote:
> On 2013-01-05 23:44, Philippe Sigaud wrote:
>
>> Well, first that would be nice to have the grammar online :)
>
> Yeah, still waiting for that pull request to be merged.
UDA can not apply to function argument?
enum attr;
void func(@attr int a) // <-- error
{
@attr int b; // <-- ok
}
|
January 06, 2013 Re: So, User-Defined Attributes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Domain | On 1/5/2013 10:57 PM, Domain wrote:
> On Saturday, 5 January 2013 at 22:57:03 UTC, Jacob Carlborg wrote:
>> On 2013-01-05 23:44, Philippe Sigaud wrote:
>>
>>> Well, first that would be nice to have the grammar online :)
>>
>> Yeah, still waiting for that pull request to be merged.
>
> UDA can not apply to function argument?
>
> enum attr;
> void func(@attr int a) // <-- error
> {
> @attr int b; // <-- ok
> }
Currently, no.
|
January 06, 2013 Re: So, User-Defined Attributes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright Attachments:
| On Sun, Jan 6, 2013 at 8:16 AM, Walter Bright <newshound2@digitalmars.com>wrote: > > >> UDA can not apply to function argument? >> >> > Currently, no. > Walter, what is the official way to return an attributed value? XXX? foo() { @Marked int i; return i; } |
January 06, 2013 Re: So, User-Defined Attributes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | On 1/6/2013 1:48 AM, Philippe Sigaud wrote:
> Walter, what is the official way to return an attributed value?
>
> XXX? foo()
> {
> @Marked int i;
> return i;
> }
>
Values do not have attributes. Attributes are attached to symbols - not values or types.
|
January 06, 2013 Re: So, User-Defined Attributes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | On 2013-01-06 10:48, Philippe Sigaud wrote: > Walter, what is the official way to return an attributed value? > > XXX? foo() > { > @Marked int i; > return i; > } Don't know if this is what you want but: struct Marked {} struct Attrs (T...) {} auto bar () { @Marked int i; return Attrs!(__traits(getAttributes, i))(); } void main () { writeln(bar()); // prints Attrs!(Marked)() } -- /Jacob Carlborg |
January 06, 2013 Re: So, User-Defined Attributes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg Attachments:
| > Don't know if this is what you want but:
>
> struct Marked {}
> struct Attrs (T...) {}
>
> auto bar ()
> {
> @Marked int i;
> return Attrs!(__traits(getAttributes, i))();
> }
>
> void main ()
> {
> writeln(bar()); // prints Attrs!(Marked)()
> }
I just want to be able to return an attributed something. How can a function return something that's attributed?
IIUC what Walter said, a function cannot return an attributed value: any
internal symbol can be attributed, but these cannot get out.
I can create can attributed value to 'catch' what a function returns, but
not automatically:
??? attributedInt()
{
@("Hello") int i = 1;
return i;
}
void main()
{
??? j = attributedInt();
// How to have j get the @("Hello") attribute?
}
The only way would be what you suggest:
- extract the attributes from the internal i
- store them in a specially-crafted struct
- return that
- in the external code, catch the returned struct
- extract the artificially stored attributes
- generate a new value with the same attributes.
Ugh.
|
January 06, 2013 Re: So, User-Defined Attributes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | On 2013-01-06 14:29, Philippe Sigaud wrote: > I just want to be able to return an attributed something. How can a > function return something that's attributed? > > IIUC what Walter said, a function cannot return an attributed value: any > internal symbol can be attributed, but these cannot get out. > I can create can attributed value to 'catch' what a function returns, > but not automatically: > > ??? attributedInt() > { > @("Hello") int i = 1; > return i; > } > > void main() > { > ??? j = attributedInt(); > // How to have j get the @("Hello") attribute? > } > > The only way would be what you suggest: > > - extract the attributes from the internal i > - store them in a specially-crafted struct > - return that > - in the external code, catch the returned struct > - extract the artificially stored attributes > - generate a new value with the same attributes. Basically, yes. I haven't been able figured out something else. -- /Jacob Carlborg |
January 06, 2013 Re: So, User-Defined Attributes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | On 2013-01-06 14:29, Philippe Sigaud wrote: > > Don't know if this is what you want but: > > struct Marked {} > struct Attrs (T...) {} > > auto bar () > { > @Marked int i; > return Attrs!(__traits(getAttributes, i))(); > } > > void main () > { > writeln(bar()); // prints Attrs!(Marked)() > } > > > I just want to be able to return an attributed something. How can a > function return something that's attributed? > > IIUC what Walter said, a function cannot return an attributed value: any > internal symbol can be attributed, but these cannot get out. > I can create can attributed value to 'catch' what a function returns, > but not automatically: > > ??? attributedInt() > { > @("Hello") int i = 1; > return i; > } > > void main() > { > ??? j = attributedInt(); > // How to have j get the @("Hello") attribute? > } > > The only way would be what you suggest: > > - extract the attributes from the internal i > - store them in a specially-crafted struct > - return that > - in the external code, catch the returned struct > - extract the artificially stored attributes > - generate a new value with the same attributes. You can perhaps encapsulate this in a mixin? -- /Jacob Carlborg |
Copyright © 1999-2021 by the D Language Foundation