www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use annotation get key name?

reply Brian <zoujiaqing gmail.com> writes:
Rust sample code:



PHP sample code:

/*
 Table(name = "users")
*/

Java sample code:

 Table(name = "users")

How to use dlang get key name?
Mar 26 2018
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Monday, 26 March 2018 at 08:29:31 UTC, Brian wrote:
 Rust sample code:



 PHP sample code:

 /*
  Table(name = "users")
 */

 Java sample code:

  Table(name = "users")

 How to use dlang get key name?
If I understand your question correctly: struct Table { string name; } struct Foo { Table("foo") int n; } unittest { import std.traits; string name = getUDAs!(Foo.n, Table)[0].name; } -- Simen
Mar 26 2018
parent reply Brian <zoujiaqing gmail.com> writes:
On Monday, 26 March 2018 at 08:50:31 UTC, Simen Kjærås wrote:
 On Monday, 26 March 2018 at 08:29:31 UTC, Brian wrote:
 Rust sample code:



 PHP sample code:

 /*
  Table(name = "users")
 */

 Java sample code:

  Table(name = "users")

 How to use dlang get key name?
If I understand your question correctly: struct Table { string name; } struct Foo { Table("foo") int n; } unittest { import std.traits; string name = getUDAs!(Foo.n, Table)[0].name; } -- Simen
Thanks. but you don't understand my means, I want have keys with multiple indeterminate names. Rust sample code: PHP sample code: /* Table(name2 = "users", name2 = "users111") */ Java sample code: Table(name2 = "users111", name1 = "users") How to use dlang get key name? Don't have support get key function? How to do?
Mar 27 2018
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 27 March 2018 at 15:38:48 UTC, Brian wrote:
 but you don't understand my means, I want have keys with 
 multiple indeterminate names.
You can pass an associative array (or better yet, a struct containing one) as a UDA and then use the regular loop over its keys and values. struct Table { string[string] keys_and_values; } Table(["name1": "users", "name2" : "users111"]) void foo() {} D's UDAs are just a value attached to the name, so all normal rules of types apply.
Mar 27 2018