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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:48:17+00:00 2026-05-26T14:48:17+00:00

i know template like T add(T)(T a, Tb){ return a + b; } But

  • 0

i know template like

T add(T)(T a, Tb){
    return  a + b;
}

But this need to user ask which type will be return, me i want compute inside method which type will be returned like:

T getField( size_t column ){
    if( column == 0 )
        T = int;
    else
        T = string;
    return to!T("1");
}

i do not know if i can cast to object and change prototype.

Thanks to all

  • 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-26T14:48:17+00:00Added an answer on May 26, 2026 at 2:48 pm

    D, like C, C++, C#, and Java is a statically typed language. All types must be known at compile time. So, no, you can’t do what you’re trying to do.

    Now, you could use inheritance, unions, or std.variant.Variant to get a form of dynamic typing, but not quite like what you’re trying to do. That only works in dynamic languages.

    • With classes, a class reference can point to an instance of a derived class rather than the base class that the reference is for, so you can get polymorphic/dynamic behavior, but the type of the base class is known at compile time, and anything that the reference refers to must be a derived type and therefore must be a class.

    • With unions, you can have one variable which can hold different types using the same memory (so it can only be one type at a time), but it’s generally considered a fairly low-level construct and best avoided if you don’t really need it. A union does not keep track of what its current type is, so if it could be both an int and a string, it’s quite easy for it to be holding an int, but you use it as a string (or vice versa), causing nasty bugs.

    • With Variant (which is probably what you want to use), you can have one variable which can hold different types – similar to a union – but you don’t specify which types that it can hold (unlike a union), and it actually keeps track of what type it currently holds (unlike a union), so it’s much safer to use.

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

Sidebar

Related Questions

I would like to know if there are any templates for doing this in
I'd like to install some presentation templates, but don't know where to put them...
I am having an issues but don't know where to solve it. My template
I have my own array class template I would like to optionally add functionality
When I add this script to a plain HTML file's HEAD section <script type=text/javascript
I want to create a Visual Studio 2010 Project Template, which is essentially a
I would like to know if there is any way to add custom behaviour
I know that you can press shift+alt+j to insert an appropriate comment template for
I have some variables inside a template and I don't know where I assigned
i have template base.html and some children templates. I don't know how to set

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.