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

  • Home
  • SEARCH
  • 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 1053975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:20:02+00:00 2026-05-16T17:20:02+00:00

When you are debugging, it’s very useful to do this: var = calc() print(var:,

  • 0

When you are debugging, it’s very useful to do this:

var = calc()
print("var:", var)

Is there any language where this is easy to do? In C and C++ you can use the stringify macro operator # and in Ruby I found this question:

Ruby – print the variable name and then its value

The solution that uses a symbol :var and a block is what I want.

In D, I used this:

void trace(alias msg)() {
    writeln(msg.stringof ~ ":" ~ to!string(msg));
}

But I’m not sure it’s the best way, because it works only in simple cases. I have tried several ways, but sometimes you can get the string, but not the value (because variables are out of scope), or you have to mixin the template first and then call the function.

And what about other languages? Python? F#? Boo? Shell script (whichever shell)? Perl? (I prefer to stay away from Perl, tho). Tcl? Lisp, Scheme? Java? (it’s highly unlikely that Java can do this).

Even in the languages where I found some kind of solution, it only works for simple cases. What if I want to print an arbitrary expression?

If I were designing a language, this feature would be a must-have. 🙂

  • 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-16T17:20:03+00:00Added an answer on May 16, 2026 at 5:20 pm

    Here’s a very general, but slightly ugly way to do it in D, using compile time function evaluation (CTFE) to generate the code as a string literal, and a mixin statement to evaluate it:

    import std.stdio, std.math;
    
    // CTFE function that generates trace code.
    string trace(string varName) {
        return "writeln(\"" ~ varName ~ ":  \", " ~ varName ~ ");";
    }
    
    void main() {
        // Trace a function call.
        mixin(trace("sqrt(5)"));
    
        // Trace a variable.
        int foo = 5;
        mixin(trace("foo"));
    }
    

    The only problems are that manually typing mixin everywhere is verbose and whatever you want to trace needs to be inside an ugly string literal.

    Note that there are two kinds of mixins in D. Template mixins are better behaved in many ways, but string mixins (used in this example) are about as general as it gets, in that any code can in principle be generated via CTFE and then mixed in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Debugging a PHP program, is there any add-on/plug-in for browser which I can view
Debugging experience http://www.dmhermitage.org/wtfborders.png This is making me want to kill myself. I have some
When debugging, I noticed there is a thread named Hashed wheel timer #1, what
For debugging purposes, I would like to display the queried interface name. I can
While debugging the code in Eclipse, I runned into the following problem. There is
Debugging assistance requested. The page http://www.freshfaves.com/newfave.html contains this code: <script> document.domain = 'freshfaves.com'; $(document).ready(function()
Debugging a binding when it is like this IsChecked={Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Mode=TwoWay,
Debugging Django on Dreamhost is proving quite the challenge. To my knowledge, print statements
For debugging purposes I need to print all public fields/properties of certain class instance.
When debugging it's quite common for me to use things such as Zend_Debug and

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.