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 450495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:52:28+00:00 2026-05-12T21:52:28+00:00

I think D’s static if is an interesting language feature. That prompts my question:

  • 0

I think D’s static if is an interesting language feature. That prompts my question: Are there are other examples of compiled languages in which the compiler has a strong notion of the code and there are languages facilities to access them?

For example, this code provides something similar to repr from Python:

char[] repr(T)(T value) {
  static if (is(typeof(value.__repr__))) { // class T provides a "repr()" method
    return value.__repr__();  
  } else static if (is(T:string)) {
    return `"` ~ value ~ `"`;
  // ...other cases...
  } else {
    return toString(value);
  }
}

I think this is cool is because it allows a different and more general approach to what overloading does, which is kind of an inside out way to make code more dynamic, compared to features like this. For example, the compiler knows how many fields my class has, but there’s no way for my code to access that information at compile time in most languages.

CAVEAT: That last paragraph had opinions in it, but I just mean to provide some motivation and clarification for my question, not elicit controversy. I just want to find out if any other compiled languages have such features.

  • 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-05-12T21:52:29+00:00Added an answer on May 12, 2026 at 9:52 pm

    Any language with real macros has a form of static if. For instance Lisp and Nemerle let you construct the code that a macro expands to using programming constructs like ‘if’ and for-loops. Those are essentially compile-time decisions and let you do something similar to static if. In the case of Nemerle macros are basically plug-ins to the compiler that are executed at compile-time.

    In C++ there’s boost MPL library which has a kind of static if that can be used to choose between two types. You could put some code inside the two types in a run() member and get something kinda similar, but with very cumbersome syntax.

    For example with Boost MPL you could do something like this:

    struct float_impl { 
        static void run() { /* float case code */ }
    }
    struct int_impl { 
        static void run() { /* int case code */ }
    }
    
    typedef typename if_<
              is_same<T, float>
            , float_impl
            , int_impl
            >::type impl_t;
    impl_t::run();
    

    In D that’d be:

    static if(is(T == float)) {
         /* float code */
    }
    else {
         /* int code */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think that I have a quite strange question. I am using a class
I think I found a bug in the static contract checking tool. Is there
Think Design: I have many applications that share the same user database! Other tables
Think of a dictionary application that grabs words upon user's click from any other
I think that handlers in android are tools to get different objects that are
I think I have a basic understanding of this, but am hoping that someone
I think this could be a very easy question for you. But I have
Think about the games like Spiral Knights and Minecraft(Which I believe both use LWJGL)
Think of the following code: static int Main() { byte[] data = File.ReadAllBytes(anyfile); SomeMethod(data);
I think that asking this might be kind of silly but I'm still wondering

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.