Thread overview
base types and toString
Aug 19, 2003
Ilya Minkov
Aug 22, 2003
Helmut Leitner
August 19, 2003
What is the D way for a generic print function?
As I tried the base types like int are not of type object.
e.g I can not use toString on an int.

Is there a way to find out to find out which type a variable has something like

type_of (var) == int of the like?

Of is there another recommended way to promote a base type to an char[]?
I could use something line sprintf, but for that I have to knw the type of some variable. So could someone give me a hand on how to do it
the D way?

Regards
Friedrich

August 19, 2003
Friedrich Dominicus wrote:
> What is the D way for a generic print function?
> As I tried the base types like int are not of type object.
> e.g I can not use toString on an int.

Looks like an omission. :)

> Is there a way to find out to find out which type a variable has something like
> 
> type_of (var) == int of the like?

D is not dynamically typed, and hence ints and other simple types are simply the type you declare them with in current scope. For classes, there is a way to get (a real ton of) type information, it's implemented but not documented yet. Burton even wrote an automatic serialisation library based on it.

You can distinguish a type when calling a function by overloading a function for multiple input types.

Yet another way is to imitate variant/dynamic typing. You create a struct which acts as a smart (discriminated) union, which can hold strings, integers, objects, and so on. Then you define conversion constructors for all types it needs to input, and this thingy is a basis for a typesafe printf, which is not in the standard library, but has been implemented elsewhere.

> Of is there another recommended way to promote a base type to an char[]?
> I could use something line sprintf, but for that I have to knw the type of some variable. So could someone give me a hand on how to do it
> the D way?

There are a number of "what may become D way" libraries in development, all scattered around different places. I think i shall go search through the newsgroup and collect them all on Helmut's WIKI.

-eye

August 22, 2003

Friedrich Dominicus wrote:
> 
> What is the D way for a generic print function?
> As I tried the base types like int are not of type object.
> e.g I can not use toString on an int.

You can find my try at this in the Venus library:
  <http://www.wikiservice.at/wiki4d/wiki.cgi?VenusLibrary>
Basically in the modules:
  - print.d (who solves the concrete problem)
  - type.d (basics for similar generic primitive interfaces)

In fact dmd 0.68 should have paved the path to make things simpler, because primitives now provide a .typeinfo property

   long l;
   TypeInfo ti=l.typeinfo;

while I had to fake this by the TypeMethods structure and
a overloaded TypeRetTpi.

So this could be reworked. The standard .typeinfo (TypeInfo)
structure could be extended with the functions of type.d
(TypeMethods) basically for
       string->type
       type->string
       string->type (options)
       type->string (formatting)
    conversions.

This should simplefy things a lot to generate "generic" functions for primitive ryoe and reduce the code to less then half of the current LOC count.

But I hadn't yet the time to do the preparations and suggest this to Walter in a proper way.

--
Helmut Leitner    leitner@hls.via.at Graz, Austria   www.hls-software.com