Module vibe.internal.meta.funcattr
Helpers for working with user-defined attributes that can be attached to function or method to modify its behavior. In some sense those are similar to Python decorator. D does not support this feature natively but it can be emulated within certain code generation framework.
Example
example
struct Context { int increment; string token; bool updated = false; } static int genID(Context* context) { static int id = 0; return (id += context.increment); } static string update(string result, Context* context) { context.updated = true; return result ~ context.token; } class API { @before!genID("id") @after!update() string handler(int id, string name, string text) { import std.string : format; return format("[%s] %s : %s", id, name, text); } } auto api = new API(); auto context = new Context(5, " | token"); auto funcattr = createAttributedFunction!(API.handler)(context); auto result = funcattr(&api.handler, "Developer", "Hello, World!"); assert (result == "[5] Developer : Hello, World! | token"); assert (context.updated);
Functions
Name | Description |
---|---|
after
|
Marks function/method for usage with `AttributedFunction `.
|
before
|
Marks function/method for usage with `AttributedFunction `.
|
createAttributedFunction
|
Syntax sugar in top of AttributedFunction
|
Structs
Name | Description |
---|---|
AttributedFunction
|
Entry point for `funcattr` API. |
Global variables
Name | Type | Description |
---|---|---|
IsAttributedParameter
|
void
|
Checks if parameter is calculated by one of attached functions. |
Authors
Михаил Страшун
Copyright
© 2013 RejectedSoftware e.K.
License
Subject to the terms of the MIT license, as written in the included LICENSE.txt file.