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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:53:51+00:00 2026-05-27T07:53:51+00:00

I have structs and classes that I’d like to be able to easily create

  • 0

I have structs and classes that I’d like to be able to easily create from strings in a generic fashion, using the to!T(string) method. However, I’m not sure how I could ‘override’ the method to get this kind of behavior. Going from my type to the string is easy (I would just define opCast(string)), but is what I’m looking for even possible?

  • 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-27T07:53:51+00:00Added an answer on May 27, 2026 at 7:53 am

    Don’t define an opCast for string if you want your type to convert to string. That’s what toString is for. writeln and format and the like use toString, not casting or to, and to will use toString, so it’s definitely better to define toString for converting to to string. You define opCast for converting to types other than string. Then you can use that with casting or with to.

    Now, if you want to convert a string to your user-defined type, then just define the appropriate constructor, and that will work with to.

    import std.conv;
    
    struct S
    {
        int i;
    
        this(string s)
        {
            i = to!int(s);
        }
    
        string toString()
        {
            return to!string(i);
        }
    }
    
    void main()
    {
        auto s = to!S("42");
        assert(s.i == 42);
        auto t = to!string(s);
        assert(t == "42");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have many structs (classes) and standalone functions that I like to compile separately
I have structs like below and when I do that initialization: ArrayList nodesMatrix =
I realize that Go does not have classes but pushes the idea of structs
In a C++ program I have two classes (structs) like struct A { int
In C++ I have the following two classes that I expose (using Boost) to
I have an array of objects that have QTTime structs as attributes. The objects
I have a bunch of classes in a CUDA project that are mostly glorified
I often take the classes that linq2sql generates and create a simple data-only class
What's your approach on writing equality checks for the structs and classes you create?
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;

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.