View previous topic :: View next topic |
Author |
Message |
nlucas
Joined: 02 Dec 2004 Posts: 1
|
Posted: Thu Dec 02, 2004 9:21 am Post subject: template and some basic type sample |
|
|
As a complete newbee to D programming I made this template test code and would like see some comments on it.
I was thinking in adding it on the tutorial pages, but first I would like to let it be reviewed by someone more experienced.
Code: |
/*
Abstract: Sample code for templates and some type info in D.
Author : Nuno Lucas
Licence : Public Domain
*/
import std.stdio;
import std.string;
/*
Generic template for integral data types
*/
template TWriteTypeInfo( T )
{
alias TWriteTypeInfo_Integral!(T) TWriteTypeInfo;
}
template TWriteTypeInfo_Integral( T )
{
void TWriteTypeInfo_Integral( )
{
writef( "?-8s ?-20s ?-20s ?-6s ?-6s ?-7s\n"
, typeid(T), T.min, T.max, T.init, T.sizeof, T.alignof );
}
}
/*
Character types must be handled a bit different than the generic way
*/
template TWriteTypeInfo( T:char ) { alias TWriteTypeInfo_Character!(char) TWriteTypeInfo; }
template TWriteTypeInfo( T:wchar ) { alias TWriteTypeInfo_Character!(wchar) TWriteTypeInfo; }
template TWriteTypeInfo( T:dchar ) { alias TWriteTypeInfo_Character!(dchar) TWriteTypeInfo; }
template TWriteTypeInfo_Character( T )
{
void TWriteTypeInfo_Character( )
{
writef( "?-8s ?#-20x ?#-20x ?#-6x ?-6s ?-7s\n"
, typeid(T), cast(uint) T.min, cast(uint) T.max
, cast(uint) T.init, T.sizeof, T.alignof );
}
}
/* Floating point types */
template TWriteTypeInfo( T:float ) { alias TWriteTypeInfo_Floating!(float) TWriteTypeInfo; }
template TWriteTypeInfo( T:double ) { alias TWriteTypeInfo_Floating!(double) TWriteTypeInfo; }
template TWriteTypeInfo( T:real ) { alias TWriteTypeInfo_Floating!(real) TWriteTypeInfo; }
template TWriteTypeInfo( T:ifloat ) { alias TWriteTypeInfo_Floating!(ifloat) TWriteTypeInfo; }
template TWriteTypeInfo( T:idouble ) { alias TWriteTypeInfo_Floating!(idouble) TWriteTypeInfo; }
template TWriteTypeInfo( T:ireal ) { alias TWriteTypeInfo_Floating!(ireal) TWriteTypeInfo; }
template TWriteTypeInfo( T:cfloat ) { alias TWriteTypeInfo_Floating!(cfloat) TWriteTypeInfo; }
template TWriteTypeInfo( T:cdouble ) { alias TWriteTypeInfo_Floating!(cdouble) TWriteTypeInfo; }
template TWriteTypeInfo( T:creal ) { alias TWriteTypeInfo_Floating!(creal) TWriteTypeInfo; }
template TWriteTypeInfo_Floating( T )
{
void TWriteTypeInfo_Floating( )
{
writef( "?-8s ?-24s ?3s ?5s ?4s ?7s ?7s \n"
, typeid(T), toString(T.epsilon), T.dig, T.mant_dig, T.sizeof
, T.min_10_exp, T.max_10_exp );
}
}
/* This is just used for printing headers */
template TWriteTypeInfo( T : void )
{
void Integral_Header( )
{
writef( "?-8s ?-20s ?-20s ?-6s ?-6s ?-7s\n"
, "type", "min", "max", "init", "sizeof", "alignof" );
writef( "-------------------------------------------------------------------------------\n" );
}
void Floating_Header( )
{
writef( "?-8s ?-24s ?-3s ?-8s ?-6s ?-10s ?-10s\n"
, "type", "epsilon", "dig", "mant_dig", "sizeof", "min_10_exp", "max_10_exp" );
writef( "-------------------------------------------------------------------------------\n" );
}
}
int main()
{
writef( "\nINTEGRAL DATA TYPES:\n\n" );
TWriteTypeInfo!(void).Integral_Header( );
TWriteTypeInfo!( bit )();
TWriteTypeInfo!( byte )();
TWriteTypeInfo!( ubyte )();
TWriteTypeInfo!( short )();
TWriteTypeInfo!( ushort )();
TWriteTypeInfo!( int )();
TWriteTypeInfo!( uint )();
TWriteTypeInfo!( long )();
TWriteTypeInfo!( ulong )();
TWriteTypeInfo!( char )();
TWriteTypeInfo!( wchar )();
TWriteTypeInfo!( dchar )();
writef( "\nFLOATING POINT DATA TYPES:\n\n" );
TWriteTypeInfo!(void).Floating_Header( );
TWriteTypeInfo!( float )();
TWriteTypeInfo!( double )();
TWriteTypeInfo!( real )();
TWriteTypeInfo!( ifloat )();
TWriteTypeInfo!( idouble )();
TWriteTypeInfo!( ireal )();
TWriteTypeInfo!( cfloat )();
TWriteTypeInfo!( cdouble )();
TWriteTypeInfo!( creal )();
return 0;
}
|
This program produces the following output:
Code: |
INTEGRAL DATA TYPES:
type min max init sizeof alignof
-------------------------------------------------------------------------------
bit false true false 1 1
byte -128 127 0 1 1
ubyte 0 255 0 1 1
short -32768 32767 0 2 2
ushort 0 65535 0 2 2
int -2147483648 2147483647 0 4 4
uint 0 4294967295 0 4 4
long -9223372036854775808 9223372036854775807 0 8 8
ulong 0 18446744073709551615 0 8 8
char 0 0xff 0xff 1 1
wchar 0 0xffff 0xffff 2 2
dchar 0 0x10ffff 0xffff 4 4
FLOATING POINT DATA TYPES:
type epsilon dig mant_dig sizeof min_10_exp max_10_exp
-------------------------------------------------------------------------------
float 1.19209e-07 6 24 4 -37 38
double 2.22045e-16 15 53 8 -307 308
real 1.0842e-19 18 64 10 -4932 4932
ifloat 1.19209e-07i 6 24 4 -37 38
idouble 2.22045e-16i 15 53 8 -307 308
ireal 1.0842e-19i 18 64 10 -4932 4932
cfloat 1.19209e-07+1.19209e-07i 6 24 8 -37 38
cdouble 2.22045e-16+2.22045e-16i 15 53 16 -307 308
creal 1.0842e-19+1.0842e-19i 18 64 20 -4932 4932
|
My aim is to know if a more simple/readable program can do the same thing (using templates, off course).
Regards,
~Nuno Lucas |
|
Back to top |
|
|
jcc7
Joined: 22 Feb 2004 Posts: 657 Location: Muskogee, OK, USA
|
Posted: Thu Dec 02, 2004 7:02 pm Post subject: Re: template and some basic type sample |
|
|
nlucas wrote: | As a complete newbee to D programming I made this template test code and would like see some comments on it.
I was thinking in adding it on the tutorial pages, but first I would like to let it be reviewed by someone more experienced. | Thanks for a cool example. It looks good to me. I think it would fit best in the "Templates" category (or maybe the "Advanced" category).
nlucas wrote: | My aim is to know if a more simple/readable program can do the same thing (using templates, off course). | I certainly could be wrong (since I don't have template experience), but I think that's as simple as it gets. Originally, Walter wasn't even planning to have any templates in D 1.0, so we're really getting more than we deserve. |
|
Back to top |
|
|
JJR
Joined: 22 Feb 2004 Posts: 1104
|
Posted: Thu Dec 02, 2004 11:55 pm Post subject: |
|
|
Welcome!
It's always good to have another template example. Generics can be pretty abstract sometimes.
I wouldn't consider this particular template example as simple. I think there could be a simpler demonstration. But this would indeed be a welcome advanced tutorial.
When it comes to templates, anything even slightly elaborate really needs accompanying text to make the learning experience complete (especially with novices). It would be even better if your example had such. But with or without, it's still a very welcome addition.
Thank you!
-John
PS. It's weird: I'm starting to feel like one of a pair of movie critics whenever I'm around with Justin. |
|
Back to top |
|
|
jcc7
Joined: 22 Feb 2004 Posts: 657 Location: Muskogee, OK, USA
|
Posted: Fri Dec 03, 2004 12:27 am Post subject: |
|
|
JJR wrote: | PS. It's weird: I'm starting to feel like one of a pair of movie critics whenever I'm around with Justin. | Oops. I think we might be saying "two thumbs up" too often. Sometime we'll have to say, "The execution was superb, but the dialog was all wrong. Don't waste your time with this one."
You're right about more comments being better, but most of the examples I've added are sparcely commented, so I'm not in a position to complain. |
|
Back to top |
|
|
JJR
Joined: 22 Feb 2004 Posts: 1104
|
Posted: Fri Dec 03, 2004 12:36 am Post subject: |
|
|
jcc7 wrote: | JJR wrote: | PS. It's weird: I'm starting to feel like one of a pair of movie critics whenever I'm around with Justin. | Oops. I think we might be saying "two thumbs up" too often. Sometime we'll have to say, "The execution was superb, but the dialog was all wrong. Don't waste your time with this one." |
Ha Ha! Exactly, we really have to get our act together! |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|