Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:33:18+00:00 2026-06-17T09:33:18+00:00

I’m trying to learn a little more about D’s compile time evaluation and understand

  • 0

I’m trying to learn a little more about D’s compile time evaluation and understand how its templates, mixins, attributes, etc all work. One thing I’d like to try and do is figure out an elegant way to mark a class’s members as being serializable or loadable from a database. In the example below, I’ve created a tuple that lists which members to use when reading or (later on) serializing the instance.

My first question is, is this a proper usage of tuples as it stands below? And secondly, if so, is there a way to generate this tuple automatically at compile time using the user defined attributes I’ve assigned to the relevant member variables? I’ve dug through the various documentation pages like http://dlang.org/attribute.html and http://dlang.org/phobos/std_traits.html but I can’t seem to figure out how to use them properly for this purpose (i.e. looping through a class’s members and determining which variables have the desired attribute). I’m also not quite certain if I have completely the wrong idea about how attributes are supposed to be used. Any suggestions on the best way to go about this would be appreciated.

enum ENCODABLE = 1;
alias string[string] Row;
template Tuple (T...) { alias T Tuple; }

class A {
    @(ENCODABLE) string name;
    @(ENCODABLE) int x;
    int* p;

    alias Tuple!("name","x") encodables;

    this(Row row) {
        foreach (var; encodables) {
            __traits(getMember, this, var) = to!(typeof(__traits(getMember, this, var)))(row[var]);
        }
    }
}

void main() {
    Row row = ["name":"Asdf", "x":"120"]; // Simulated database row
    auto a = new A(row);
    writefln("%s,%d,%d", a.name, a.x, a.p); // Asdf,120,null
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:33:19+00:00Added an answer on June 17, 2026 at 9:33 am

    I’ve managed to get it working with the following code, and a little help from the isValueInTuple template based on code provided in CyberShadow’s answer. It still feels a bit clunky, but seems to get the job done. Comments/criticism welcome if I’m doing something horrible against the nature of templates!

    enum {
        ENCODABLE = "ENCODABLE",
    };
    alias string[string] Row;
    template Tuple(T...) { alias T Tuple; }
    template isValueInTuple(string s, T...) {
        static if (T.length == 0) {
            enum bool isValueInTuple = false;
        } else static if (T.length == 1) {
            static if (is(typeof(T[0]) == typeof(s))) {
                enum bool isValueInTuple = T[0] == s;
            } else {
                enum bool isValueInTuple = false;
            }
        } else {
            enum bool isValueInTuple = isValueInTuple!(s, T[0]) || isValueInTuple!(s, T[1..$]);
        }
    }
    template GenEncodables(U) {
        string GenEncodables() {
            string ret = "alias Tuple!(";
            int fn = 0;
            foreach (index, field; __traits(allMembers, U)) {
                static if (field != "Monitor") { // better way to avoid compilation errors here?
                    static if (isAssignable!(typeof(mixin(U.stringof~"."~field)))) {
                        static if (isValueInTuple!(ENCODABLE, __traits(getAttributes, mixin(U.stringof~"."~field)))) {
                            if (fn++)
                                ret ~= ",";
                            ret ~= `"`~field~`"`;
                        }
                    }
                }
            }
            ret ~= ") encodables;";
            return ret;
        }
    }
    mixin template Encodables() {
        mixin(GenEncodables!(typeof(this)));
    }
    
    
    class A {
        @ENCODABLE string name;
        @ENCODABLE int x;
        int *p;
    
        this() {}
    
        mixin Encodables; // must come after this() definition, apparently!
    
        this(Row row) {
            foreach (var; encodables) {
                pragma(msg, "Reading parameter "~var~" from row");
                __traits(getMember, this, var) = to!(typeof(__traits(getMember, this, var)))(row[var]);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.