www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What have you done with UDAs?

reply "weaselcat" <weaselcat gmail.com> writes:
I never seem to use them for anything, has anyone else done 
anything interesting with them?
Jun 22 2015
next sibling parent "Dragos Carp" <dragoscarp gmail.com> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
https://github.com/linkrope/dunit#user-defined-attributes
Jun 22 2015
prev sibling next sibling parent reply "Ilya Yaroshenko" <ilyayaroshenko gmail.com> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
UDAs are very useful for IO libraries. You can find them in vibe.d web server: http://d.readthedocs.org/en/latest/examples.html#service (see last method)
Jun 22 2015
parent Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
On 06/22/2015 03:18 PM, Ilya Yaroshenko wrote:
 On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done anything
 interesting with them?
UDAs are very useful for IO libraries. You can find them in vibe.d web server: http://d.readthedocs.org/en/latest/examples.html#service (see last method)
Yea, I really like vibe's use of them. I've worked with web tools before that inferred URLs and querty string value names and visibility from the names/visibility of classes/functions/arguments. But I never liked that approach, how it conflates internal names/visibility with external interface. Too much implicit magic, and too much leaking of internals. UDA'a maintain the convenience of that approach, but keep a nice clean separation.
Jun 22 2015
prev sibling next sibling parent reply "Alex Parrill" <initrd.gz gmail.com> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
I'm writing a program that can accept subcommands via either the command line (ex. `prog mycmd 1 2`) or via a shell. I represent each command as a function, use UDAs to store the help text and if they can be ran from the command line or shell. The command list, full help text, and command dispatcher is generated at compile time via templates, CTFE, and static foreach. An example command: ("<nanoseconds>") // arguments ("Sets the amount of time to increment the clock on each frame.") // description ShellOnly // can't be ran from command line int cmd_set_time_per_frame(string[] args) { // ... }
Jun 22 2015
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 22-Jun-2015 22:21, Alex Parrill wrote:
 On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done anything
 interesting with them?
I'm writing a program that can accept subcommands via either the command line (ex. `prog mycmd 1 2`) or via a shell. I represent each command as a function, use UDAs to store the help text and if they can be ran from the command line or shell. The command list, full help text, and command dispatcher is generated at compile time via templates, CTFE, and static foreach.
And I thought that I might be the only one doing this ;) Declarative command-line handling is much more scalable approach for swiss-army knife command-line tools.
 An example command:

  ("<nanoseconds>") // arguments
  ("Sets the amount of time to increment the clock on each frame.") //
 description
  ShellOnly // can't be ran from command line
 int cmd_set_time_per_frame(string[] args) {
      // ...
 }
Awesome. Is it open-sourced? How about handling argument conversion automatically (via to! and/or custom functions)? Say : ("<num1> <num2>") CmdName("plus") void add(int a, int b) { writelen(a+b); } To be automagically callable like: ./prog plus 2 4 -- Dmitry Olshansky
Jun 23 2015
parent reply "Alex Parrill" <initrd.gz gmail.com> writes:
On Tuesday, 23 June 2015 at 14:52:30 UTC, Dmitry Olshansky wrote:
 An example command:

  ("<nanoseconds>") // arguments
  ("Sets the amount of time to increment the clock on each 
 frame.") //
 description
  ShellOnly // can't be ran from command line
 int cmd_set_time_per_frame(string[] args) {
      // ...
 }
Awesome. Is it open-sourced? How about handling argument conversion automatically (via to! and/or custom functions)? Say : ("<num1> <num2>") CmdName("plus") void add(int a, int b) { writelen(a+b); } To be automagically callable like: ./prog plus 2 4
Not yet; its ATM a bit coupled with the application I'm writing. Specifying types with the arguments is definitely feasible, but I haven't gotten around to writing it yet. You could also possibly specify flag arguments (--foo) by specifying default parameters. (somewhat related: std.getopts is kinda bad; you can't get help text without successfully parsing arguments and `required` breaks `--help`)
Jun 23 2015
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 6/23/15 8:05 AM, Alex Parrill wrote:
 (somewhat related: std.getopts is kinda bad; you can't get help text
 without successfully parsing arguments and `required` breaks `--help`)
Could you please submit these as bug reports at http://issues.dlang.org - thanks! -- Andrei
Jun 23 2015
prev sibling next sibling parent "Atila Neves" <atila.neves gmail.com> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
I use them all the time: https://github.com/atilaneves/unit-threaded https://github.com/atilaneves/cerealed https://github.com/atilaneves/unencumbered https://github.com/D-Programming-Language/phobos/pull/3207 (std.experimental.testing proposal) Compile-time reflection seems to be my D hammer. Atila
Jun 22 2015
prev sibling next sibling parent reply "Kapps" <opantm2+spam gmail.com> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
Like several others, I use them for generating command line interfaces to set variables / invoke methods using reflection. For example https://shardsoft.com/stash/projects/SHARD/repos/dap/browse/source D Asset Pipeline Converts assets into an intermediate post-processed format more efficiently loaded at runtime. Commands: [--init]: Initializes the folder structure for a dap repository using a Content subdirectory in this folder. [--clean]: Cleans the output directory, removing all built assets. [-h|--help]: Displays the help string. [-a|--add]: Adds the given raw asset to the asset store using the default processor and default settings. [-r|--remove]: Removes the asset with the specified qualified name from the asset store. [-l|--list]: Lists all assets currently stored. [-b|--build]: Builds all dirty assets using current settings. [-i|--inspect]: Shows all properties of the given asset. [-o|--open]: Opens the given asset in an external editor. [-m|--modify]: Modifies a property of a processor on an asset, or the processor used to build the asset. Options: [--input-folder]: The folder that assets should be read from and settings stored in. Default: Content/Input [--output-folder]: The folder that generated assets should be saved to. Default: Content/Output [--log-level]: The minimum severity for a message to be logged. Default: info
Jun 22 2015
parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Monday, 22 June 2015 at 19:36:59 UTC, Kapps wrote:
 On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
Like several others, I use them for generating command line interfaces to set variables / invoke methods using reflection.
Same here: http://blog.thecybershadow.net/2014/08/05/ae-utils-funopt/
Jun 23 2015
prev sibling next sibling parent reply "Tofu Ninja" <emmons0 purdue.edu> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
I am currently writing a ui system that defines the majority of the layout in a dsl. The ui element types are defined in D code and I use uda's to mark which fields can be accesses by the dsl.
Jun 22 2015
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 23/06/2015 7:43 a.m., Tofu Ninja wrote:
 On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done anything
 interesting with them?
I am currently writing a ui system that defines the majority of the layout in a dsl. The ui element types are defined in D code and I use uda's to mark which fields can be accesses by the dsl.
Got a repo yet? Would be interested in reading more about it.
Jun 22 2015
parent "Tofu Ninja" <emmons0 purdue.edu> writes:
On Tuesday, 23 June 2015 at 06:43:21 UTC, Rikki Cattermole wrote:
 I am currently writing a ui system that defines the majority 
 of the
 layout in a dsl. The ui element types are defined in D code 
 and I use
 uda's to mark which fields can be accesses by the dsl.
Got a repo yet? Would be interested in reading more about it.
Not yet, it's integrated into a game engine I am working on and it's in the early stages of usability, will probably split it off once it gets more mature.
Jun 23 2015
prev sibling next sibling parent "extrawurst" <stephan extrawurst.org> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
They are great! see my post about one use case: http://extrawurst.github.io/dlang/metaprogramming/imgui/2015/05/28/dlang-annotations.html
Jun 22 2015
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 22/06/15 21:09, weaselcat wrote:
 I never seem to use them for anything, has anyone else done anything
 interesting with them?
Serialization: https://github.com/jacob-carlborg/orange/blob/master/tests/NonSerialized.d#L25-L39 Selectors in D/Objective-C. It's actually a compiler recognized UDA, but still a UDA: https://github.com/jacob-carlborg/dmd/blob/dobjc_instance_methods/test/runnable/objc_call.d#L7 -- /Jacob Carlborg
Jun 22 2015
prev sibling next sibling parent reply "Jonas Drewsen" <nospam4321 hotmail.com> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
I use a few in the deadcode editor e.g.: Function can be called by a menu entry: MenuItem("Edit/Copy") Function can be called by a shortcut: Shortcut("<shift> + <f10>") Function should run in a fiber when called: InFiber
Jun 22 2015
parent "Jonas Drewsen" <nospam4321 hotmail.com> writes:
On Monday, 22 June 2015 at 20:29:15 UTC, Jonas Drewsen wrote:
 On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
I use a few in the deadcode editor e.g.: Function can be called by a menu entry: MenuItem("Edit/Copy") Function can be called by a shortcut: Shortcut("<shift> + <f10>") Function should run in a fiber when called: InFiber
Just remembered, I also use it for the animation system to tell what can be animated (modified by proxy) e.g: Bindable() { CSSPositionMix _position; CSSScaleMix _width; CSSScaleMix _height; CSSVisibility _visibility; ... }
Jun 25 2015
prev sibling next sibling parent reply "Brian Schott" <briancschott gmail.com> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
Semi-hijacking this thread: I recently made a few pull requests to get better support for UDAs into Phobos. https://github.com/D-Programming-Language/phobos/pull/3356 https://github.com/D-Programming-Language/phobos/pull/3421 https://github.com/D-Programming-Language/phobos/pull/3363 The last of the three is stuck because nobody's looking at it. Can those of you who use UDAs take a look at these and confirm that I haven't made any dumb mistakes before the 2.068 release goes out with them included?
Jun 22 2015
parent reply "extrawurst" <stephan extrawurst.org> writes:
On Monday, 22 June 2015 at 20:57:59 UTC, Brian Schott wrote:
 On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
Semi-hijacking this thread: I recently made a few pull requests to get better support for UDAs into Phobos. https://github.com/D-Programming-Language/phobos/pull/3356 https://github.com/D-Programming-Language/phobos/pull/3421 https://github.com/D-Programming-Language/phobos/pull/3363 The last of the three is stuck because nobody's looking at it. Can those of you who use UDAs take a look at these and confirm that I haven't made any dumb mistakes before the 2.068 release goes out with them included?
LGTM! Finally these are added to phobos. Thanks!
Jun 23 2015
parent Mathias Lang via Digitalmars-d <digitalmars-d puremagic.com> writes:
I use them all the time in vibe.web.rest. It allows to override the
defaults (e.g. by default `getfoo` maps to `GET /foo`):
https://github.com/Geod24/vibe.d/blob/ea061223ab96d5acdc004e9369f904c5e25ec8d0/source/vibe/web/rest.d#L60-L66
I actually really, really miss attributed parameters. Obviously it's
possible to emulate it (
https://github.com/Geod24/vibe.d/blob/ea061223ab96d5acdc004e9369f904c5e25ec8d0/source/vibe/web/rest.d#L169-L182)
but it isn't as nice...

2015-06-23 9:04 GMT+02:00 extrawurst via Digitalmars-d <
digitalmars-d puremagic.com>:

 On Monday, 22 June 2015 at 20:57:59 UTC, Brian Schott wrote:

 On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:

 I never seem to use them for anything, has anyone else done anything
 interesting with them?
Semi-hijacking this thread: I recently made a few pull requests to get better support for UDAs into Phobos. https://github.com/D-Programming-Language/phobos/pull/3356 https://github.com/D-Programming-Language/phobos/pull/3421 https://github.com/D-Programming-Language/phobos/pull/3363 The last of the three is stuck because nobody's looking at it. Can those of you who use UDAs take a look at these and confirm that I haven't made any dumb mistakes before the 2.068 release goes out with them included?
LGTM! Finally these are added to phobos. Thanks!
Jun 23 2015
prev sibling next sibling parent "E.S. Quinn" <anonymous example.com> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
I use UDAs in my project to generate GTK UI for editing components in my ECS. (For things like marking string fields as filenames, or giving numeric fields upper and lower bounds) https://dl.dropboxusercontent.com/u/187059/Screenshot-sarah%20-%20Entity%20Editor.png
Jun 22 2015
prev sibling next sibling parent "Basile Burg" <basile.burg gmx.com> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
I use them to annotate the setter/getter pairs in classes. Two analyzers look for them to automatically create what i call a "property descriptor". https://github.com/BBasile/iz/blob/master/import/iz/properties.d#L362 Property descriptors are then used to - bind/synchronize several properties together. - serialize or deserialize classes. This is directly inspired by Pascal's "published" property. All the type information system is replaced with UDA and static analysis: Pascal: --- published property myProp: integer read getMyProp write setMyProp; --- D2 equivalent: --- SetGet private int _field; // solution 1: use directly the field Set void myProp(int value){} // solution 2: use explicit accessors. Get int myProp(){return _field;} --- The Annotations work very well with virtual methods. For example the most derivated (overriden) anotated Set/ Get is always took by the analyzer, like expected.
Jun 22 2015
prev sibling next sibling parent Manu via Digitalmars-d <digitalmars-d puremagic.com> writes:
On 23 June 2015 at 05:09, weaselcat via Digitalmars-d
<digitalmars-d puremagic.com> wrote:
 I never seem to use them for anything, has anyone else done anything
 interesting with them?
I've used them for attributing struct members with some information about their semantic meaning, which allows systems to automatically bind those structs to useful functions. For instance, I have attributed color structs with information about the D3D or OpenGL format spec's that represent, I have done the same with vertex data, expressing the semantic meaning of the elements in the vertex struct, so they can be bound automatically to the appropriate shader inputs. I've used it for control over serialisation of data in structs. I've used it to tag information to struct fields that are to be exported to editor environments, and control how those properties should be represented in editors (pretty-names, type of editor (select box, color picker, etc). I've used them for giving information to automated bindings for scripting systems. Particularly, I use them to tell automated systems to consider or ignore certain things.
Jun 22 2015
prev sibling next sibling parent Rikki Cattermole <alphaglosined gmail.com> writes:
On 23/06/2015 7:09 a.m., weaselcat wrote:
 I never seem to use them for anything, has anyone else done anything
 interesting with them?
Dvorm and Cmsed used them. Both for routing and data models. (They are both sunsetted right now btw)
Jun 22 2015
prev sibling next sibling parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
I use them in a simple MongoDB-based ORM, not (yet?) publically 
available. Usage is like this:

 Collection("my.model")
struct MyModel {
      Field  Required  Index
     BsonObjectID foreignKey;

      Field
     string someOptionalString;
      Field  Required
     int[] someRequiredArray;

      Field  Validate!(a => a % 2 != 0)
     int onlyOddIntegers;
      Field  ValidEmail
     string email;

     mixin ORM;
}
Jun 23 2015
prev sibling next sibling parent "ponce" <contact gam3sfrommars.fr> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
I've used them along with reflection to describe OpenGL vertex formats from a struct. https://github.com/d-gamedev-team/gfm/blob/master/opengl/gfm/opengl/vertex.d#L25 From this thread it seems UDA are quite successful.
Jun 23 2015
prev sibling next sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
Amongst other things, I use them in PydMagic to mark functions to be wrapped by pyd and exposed to the python.
Jun 23 2015
prev sibling parent "Random D-user" <no email.com> writes:
On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
 I never seem to use them for anything, has anyone else done 
 anything interesting with them?
What a cool thread. This is very valuable info for new(ish) d users. It's often the case for language features that they are just branded cool. But that's just a statement (and you go 'uhm, ok.'). What people usually need is some kind of proof or demonstration why they are useful and how to solve problems with them (which you can usually only get through experience). Wouldn't it be great if there was a wiki page of cool d-features where each feature would have a list like this showcasing what people have done with it? You could then just point people to this wiki page to get a practical view of why d is great.
Jun 23 2015