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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:59:20+00:00 2026-05-23T02:59:20+00:00

I recently asked this question about how to simulate type classes in D and

  • 0

I recently asked this question about how to simulate type classes in D and suggested a way to do this using template specialization.

I discovered that D doesn´t recognize template specialization in a different source file. So I couldn´t just make a specialization in a file not included from the file where the generic function is defined. To illustrate, consider this example:

//template.d
import std.stdio;
template Generic(A) {
  void sayHello() {
    writefln("Generic");
  }
}

void testTemplate(A)() {
    Generic!A.sayHello();
}


//specialization.d
import std.stdio;
import Template;

template Generic(A:int) {
  void sayHello() {
      writefln("only for ints");
  }
}

void main() {
    testTemplate!int();
}

This code prints “generic” when I run it. So I´m asking whether there is some good workaround, so that the more specialized form can be used from the algorithm.

The workaround I used in the question about Type classes was to mixin the generic functions after importing all files with template specialization, but this is somewhat ugly and limited.

I heard c++1x will have extern templates, which will allow this. Does D have a similar feature?

  • 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-23T02:59:21+00:00Added an answer on May 23, 2026 at 2:59 am

    I think I can give a proper answer to this question. No.

    What you are trying to do is highjack the functionality of template.d (also case should match on file and import Template, some operating systems it matters). Consider:

    // template.d
    ...
    
    // spezialisation.d
    import std.stdio;
    import template;
    
    void main() {
        testTemplate!int();
    }
    

    Now someone updates the code:

    // specialization.d
    import std.stdio;
    import template;
    import helper;
    
    void main() {
        testTemplate!int();
        getUserData();
    }
    

    Perfect right? well inside helper:

    // helper.d
    getUserData() { ... }
    
    
    template Generic(A:int) {
        A placeholder; //...
    }
    

    You have now changed the behavior of specialization.d just from an import and in fact this would fail to compile as it can not call sayHello. This highjack prevention does have its issues. For example you may have a function which takes a Range, but the consumer of your library can not pass an array unless your library imports std.array since this is where an array is “transformed” into a range.

    I do not have a workaround for your problem.

    Michal’s comment provides a solution to the second form of highjacking, where say specialization.d tried to highjack getUserData

    // specialization.d
    import std.stdio;
    import template;
    import helper;
    
    alias helper.getUserData getUserData;
    
    string getUserData(int num) { ... }
    
    void main() {
        testTemplate!int();
        getUserData();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently asked this question and worked out that the reason a single email
I recently asked a question about IIf vs. If and found out that there
I recently asked a question about Oracle Encryption. Along the way to finding a
I recently asked a question about tracing Linq-to-Entities I think that one of the
I recently asked a question about using the Page.User or HttpContext.Current.User in the View.
I recently asked a question about using Fluent NHibernate with .NET 4 - I
I recently asked a question about the same function, that solved my probelem and
This is related to another question I recently asked about storing a non-bare repository
I recently asked this question: Expose XML or Objects - thanks all for the
This question is related to this other question I recently asked... cf.net exception 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.