www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - somthing c++ can to that D can't

reply BCS <BCS_member pathlink.com> writes:
While checking out the NWCPP website I ran across the following:

http://www.nwcpp.org/Downloads/2005/twoViews.pdf

Ignore the first part, it's just a long and involved way of showing D is way
better than C++.

The neat thing is part 2 ("Dimensional Analysis in C++"). The gist of it is have
a type with 0 runtime overhead that enforces unit correctness. A while ago I
uses this exact problem as an argument for implicit template definitions. Seeing
as D is one of the few programming platforms that support 80 bit reals, having
this ability would make it a fantastic scientific/engineering tool. 

If D had this ability (implicit template specialization) I would be willing to
put a substantial amount of time into programming a template to leverage it
against this problem.
Nov 07 2005
parent reply Oskar Linde <Oskar_member pathlink.com> writes:
In article <dkos5c$1135$1 digitaldaemon.com>, BCS says...
While checking out the NWCPP website I ran across the following:

http://www.nwcpp.org/Downloads/2005/twoViews.pdf

Ignore the first part, it's just a long and involved way of showing D is way
better than C++.
I think the first part raises some interesting points. He talks about the many different types of polymorphism C++ has: * Function overloading * Implicit template instantiation * Template specialization * Inheritance Combined with: * Implicit type casts * Promotion rules * Conversion rules * Type modifiers For different types of polymorphism, C++ has different and complicated rules. While D doesn't have type modifiers, implicit type casts (which can be very useful when used carefully) and implicit template instantiation (yet), it still shares those other issues with C++. D does not do much to unify the different types of polymorphism (unlike Dylan for instance). Implementing implicit template instantiation right doesn't seem very easy. Some of the C++ pitfalls will probably be implemented aswell.
The neat thing is part 2 ("Dimensional Analysis in C++"). The gist of it is have
a type with 0 runtime overhead that enforces unit correctness. A while ago I
uses this exact problem as an argument for implicit template definitions. Seeing
as D is one of the few programming platforms that support 80 bit reals, having
this ability would make it a fantastic scientific/engineering tool. 

If D had this ability (implicit template specialization) I would be willing to
put a substantial amount of time into programming a template to leverage it
against this problem.
Why is template specialization essential? Only the most simple type of implicit template instantiation (one template, one argument, no implicit casts, no specialization) would be needed in D to support something like: // Base alias DimensionSystem3D!(1,0,0) Length; alias DimensionSystem3D!(0,1,0) Time; alias DimensionSystem3D!(0,0,1) Mass; // Derived alias DimensionSystem3D!(1,-1,0) Velocity; alias DimensionSystem3D!(1,-2,0) Acceleration; alias DimensionSystem3D!(2,0,0) Area; alias DimensionSystem3D!(3,0,0) Volume; alias DimensionSystem3D!(-3,0,1) Density; alias DimensionSystem3D!(1,-2,1) Force; alias DimensionSystem3D!(-1,-2,1) Pressure; alias DimensionSystem3D!(2,-2,1) Moment; alias DimensionSystem3D!(0,0,0) Unitless; .. // Usage Length l = Length(10); Time t = Time(5); Mass m = Mass(0.005); Velocity v = l/t; auto something = t*t/l/m; Force f = Unitless(1) / something; Although this is very useful for scientific programming, I think a more common usage case for dimensional correct code is already supported in D by the very powerful feature: strong typedefs. While not as powerful as above, strong typedefs could be used much more than today to separate common dimensions like: * array length * memory size * screen coordinates * counters * and many more... / Oskar
Nov 08 2005
next sibling parent reply Sean Kelly <sean f4.ca> writes:
Oskar Linde wrote:
 Why is template specialization essential?
 
 Only the most simple type of implicit template instantiation (one template, one
 argument, no implicit casts, no specialization) would be needed in D to support
 something like:
 
 // Base
 alias DimensionSystem3D!(1,0,0)  Length;
 alias DimensionSystem3D!(0,1,0)  Time;
 alias DimensionSystem3D!(0,0,1)  Mass;
 
 // Derived
 alias DimensionSystem3D!(1,-1,0)  Velocity;
 alias DimensionSystem3D!(1,-2,0)  Acceleration;
 alias DimensionSystem3D!(2,0,0)   Area;
 alias DimensionSystem3D!(3,0,0)   Volume;
 alias DimensionSystem3D!(-3,0,1)  Density;
 alias DimensionSystem3D!(1,-2,1)  Force;
 alias DimensionSystem3D!(-1,-2,1) Pressure;
 alias DimensionSystem3D!(2,-2,1)  Moment;
 alias DimensionSystem3D!(0,0,0)   Unitless;
 
 ..
 
 // Usage
 Length l = Length(10);
Already possible, pretty much, as D has support for templated typedefs while C++ does not. ie. import std.c.stdio; class Map(T,U) { T key; U val; } template IntMap(U) { alias Map!(int,U) IntMap; } void main() { auto cmap = new Map!(char, char)(); auto imap = new IntMap!(char)(); printf( "%.*s\t%.*s\n%.*s\t%.*s\n", typeid( typeof( cmap.key ) ).toString(), typeid( typeof( cmap.val ) ).toString(), typeid( typeof( imap.key ) ).toString(), typeid( typeof( imap.val ) ).toString() ); } Sean
Nov 08 2005
parent reply Oskar Linde <Oskar_member pathlink.com> writes:
In article <dkquda$7m4$1 digitaldaemon.com>, Sean Kelly says...
Oskar Linde wrote:
 Why is template specialization essential?
 
 Only the most simple type of implicit template instantiation (one template, one
 argument, no implicit casts, no specialization) would be needed in D to support
 something like:
 
 // Base
 alias DimensionSystem3D!(1,0,0)  Length;
 alias DimensionSystem3D!(0,1,0)  Time;
 alias DimensionSystem3D!(0,0,1)  Mass;
 
 // Derived
 alias DimensionSystem3D!(1,-1,0)  Velocity;
 alias DimensionSystem3D!(1,-2,0)  Acceleration;
 alias DimensionSystem3D!(2,0,0)   Area;
 alias DimensionSystem3D!(3,0,0)   Volume;
 alias DimensionSystem3D!(-3,0,1)  Density;
 alias DimensionSystem3D!(1,-2,1)  Force;
 alias DimensionSystem3D!(-1,-2,1) Pressure;
 alias DimensionSystem3D!(2,-2,1)  Moment;
 alias DimensionSystem3D!(0,0,0)   Unitless;
 
 ..
 
 // Usage
 Length l = Length(10);
Already possible, pretty much, as D has support for templated typedefs while C++ does not. ie.
[snip example] Thats very neat. Never thought of that. Thanks! But the case that needs implicit template instantiation is some lines further down: Velocity v = l/t; I can't think of any way of avoiding this here. One need a template T(int x, int y), such that: T!(a1,a2) a; T!(b1,b2) b; typeof(a*b) is T!(a1+b1,a2+b2) typeof(a/b) is T!(a1-b1,a2-b2) /Oskar
Nov 08 2005
parent reply Sean Kelly <sean f4.ca> writes:
Oskar Linde wrote:
 
 But the case that needs implicit template instantiation is some lines further
 down:
 
 Velocity v = l/t;
 
 I can't think of any way of avoiding this here.
 
 One need a template T(int x, int y), such that:
 
 T!(a1,a2) a;
 T!(b1,b2) b;
 
 typeof(a*b) is T!(a1+b1,a2+b2)
 typeof(a/b) is T!(a1-b1,a2-b2) 
Yup, this kind of thing is currently not possible in D. Walter has indicated that it will probably be implemented in some form eventually, but I gather that it's fairly complicated so it's likely not a pre-1.0 feature. This example looks familiar to me for some reason... was the subject of this paper once published in CUJ? Sean
Nov 08 2005
parent reply Oskar Linde <Oskar_member pathlink.com> writes:
In article <dkr2ks$g92$1 digitaldaemon.com>, Sean Kelly says...
Oskar Linde wrote:
 
 But the case that needs implicit template instantiation is some lines further
 down:
 
 Velocity v = l/t;
 
 I can't think of any way of avoiding this here.
 
 One need a template T(int x, int y), such that:
 
 T!(a1,a2) a;
 T!(b1,b2) b;
 
 typeof(a*b) is T!(a1+b1,a2+b2)
 typeof(a/b) is T!(a1-b1,a2-b2) 
Yup, this kind of thing is currently not possible in D. Walter has indicated that it will probably be implemented in some form eventually, but I gather that it's fairly complicated so it's likely not a pre-1.0 feature. This example looks familiar to me for some reason... was the subject of this paper once published in CUJ?
I'm not sure. But there are libraries for C++ that work like this. Two of them appeared after discussion on the boost-dev list about 20 months ago. I can't find a link right now. About this being complicated: I think this is very simple as it just needs the most simple kind of implicit template instantiation. That is, you only have one single template to match, one single parameter, one argument. I guess the hard part about implicit template instantiation are rules defining all other cases with template specialization and other forms of polymorphism. /Oskar
Nov 08 2005
next sibling parent Larry Evans <cppljevans cos-internet.com> writes:
On 11/09/2005 01:06 AM, Oskar Linde wrote:
 In article <dkr2ks$g92$1 digitaldaemon.com>, Sean Kelly says...
[snip]
feature.  This example looks familiar to me for some reason... was the 
subject of this paper once published in CUJ?
I'm not sure. But there are libraries for C++ that work like this. Two of them appeared after discussion on the boost-dev list about 20 months ago. I can't find a link right now.
http://www.boost-consulting.com/metaprogramming-book.html contains section 3.1 titled "Dimensional Analysis" which is on this subject.
Nov 09 2005
prev sibling parent Larry Evans <cppljevans cos-internet.com> writes:
On 11/09/2005 01:06 AM, Oskar Linde wrote:
[snip]
 I'm not sure. But there are libraries for C++ that work like this. Two of them
 appeared after discussion on the boost-dev list about 20 months ago. I can't
 find a link right now.
There's also one in 2003: http://thread.gmane.org/gmane.comp.lib.boost.devel/91638 and in 2005: http://thread.gmane.org/gmane.comp.lib.boost.devel/116875
Nov 09 2005
prev sibling parent BCS <BCS_member pathlink.com> writes:
Are you saying that what I proposed is already possible? 
If so how? 
Would the type be a struct, class or typedef or what?
Would it be able to handle new unit types without the user defining them?

some remarks:

- Classes would NOT be good because they will probably use function calls for
math operations and kill runtimes.
- New unit types w/o user intervention are a must. There are literally an
unlimited number of units that can occur in intermediary calculations.


In article <dkqq3j$4o$1 digitaldaemon.com>, Oskar Linde says...
Why is template specialization essential?

Only the most simple type of implicit template instantiation (one template, one
argument, no implicit casts, no specialization) would be needed in D to support
something like:

// Base
alias DimensionSystem3D!(1,0,0)  Length;
alias DimensionSystem3D!(0,1,0)  Time;
alias DimensionSystem3D!(0,0,1)  Mass;

// Derived
alias DimensionSystem3D!(1,-1,0)  Velocity;
alias DimensionSystem3D!(1,-2,0)  Acceleration;
alias DimensionSystem3D!(2,0,0)   Area;
alias DimensionSystem3D!(3,0,0)   Volume;
alias DimensionSystem3D!(-3,0,1)  Density;
alias DimensionSystem3D!(1,-2,1)  Force;
alias DimensionSystem3D!(-1,-2,1) Pressure;
alias DimensionSystem3D!(2,-2,1)  Moment;
alias DimensionSystem3D!(0,0,0)   Unitless;

..

// Usage
Length l = Length(10); 
Time t = Time(5);      
Mass m = Mass(0.005);  
Velocity v = l/t;
auto something = t*t/l/m;
Force f = Unitless(1) / something;

Although this is very useful for scientific programming, I think a more common
usage case for dimensional correct code is already supported in D by the very
powerful feature: strong typedefs.

While not as powerful as above, strong typedefs could be used much more than
today to separate common dimensions like:

* array length
* memory size
* screen coordinates
* counters
* and many more...

/ Oskar
Nov 08 2005