Annotation of functions

bauss jj_1337 at live.dk
Tue Feb 20 13:40:16 UTC 2018


On Tuesday, 20 February 2018 at 13:39:17 UTC, bauss wrote:
> On Tuesday, 20 February 2018 at 12:55:31 UTC, psychoticRabbit 
> wrote:
>> On Tuesday, 20 February 2018 at 12:45:25 UTC, rikki cattermole 
>> wrote:
>>>
>>> string creater() pure {
>>> 	return "void func() {}";
>>> }
>>>
>>> mixin(creator());
>>>
>>> That is why. There are plenty of functions, classes and 
>>> structs that simply won't exist in the form of syntax until 
>>> you execute CTFE.
>>
>> I think I'd fire anyone that wrote functions in that way ;-)
>>
>
> Why would you fire someone for writing idiomatic D?
>
> It was kind of a bad example given, but there are legitimate 
> reasons to generate functions like that.
>
> Ex.
>
> mixin template Property(T, string name)
> {
>     mixin("private T _" ~ name ~ ";");
>
>     @property
>     {
>         mixin("T " ~ name ~ "() { return _" ~ name ~ "; }");
>
>         mixin("void " ~ name ~ "(T newValue) { _" ~ name ~ " = 
> newValue; }");
>     }
> }
>
> mixin template ReadProperty(T, string name)
> {
>     mixin("private T _" ~ name ~ ";");
>
>     @property
>     {
>         mixin("T " ~ name ~ "() { return _" ~ name ~ "; }");
>     }
> }
>
> mixin template WriteProperty(T, string name)
> {
>     mixin("private T _" ~ name ~ ";");
>
>     @property
>     {
>         mixin("void " ~ name ~ "(T newValue) { _" ~ name ~ " = 
> newValue; }");
>     }
> }

I should probably have put an example usage to show how it's used:

class Foo
{
     mixin Property!(int, "bar");

     mixin Property!(string, "baz");
}

void main()
{
     auto foo = new Foo;
     foo.bar = 100;
     foo.baz = "Hello";

     import std.stdio;

     writeln(foo.bar);
     writeln(foo.baz);
}


More information about the Digitalmars-d mailing list