I am always fully prepared to be shot down by experts on this forum, so I thought I would mention my wee idea here first. Maybe there is a way around this problem that I wasn't aware of.
My motivation for doing this comes from my field of Surveying where there are lots of examples of potential types which consist of a solitary double. Also I was reading one of Bjarne's articles about mission critical systems: would these benefit from making it easier to have very strong typing for function arguments? More about this later.
Now, a use case: There are lots of ways to create a geometric Arc, and we need a way to disambiguate arguments in the constructors. The arguments consist of 2 types: doubles and points. Obviously the easiest thing to do is create structs for each different type, but this is a pain when the object contains a solitary double as in Radius, StartAngle, Angle, EndAngle. It is even worse for the points: Sure, one can invent new classes that inherit from an existing Point class, but it would be convenient to avoid that as well. I am aware of class factory design patterns exist, but am proposing this could be a language feature.
Obviously the existing
typedef
&
using
alias don't work for this.
So I am proposing these
new syntaxes:
First for simple types
1 2 3 4 5 6 7 8 9
|
// with a simple type double
typedef class CRadius<double>;
// with assignment
typedef class CRadius<double> = 10.0;
//or this with a constructor
typedef class CRadius<double>(10.0);
|
which would implicitly create this:
1 2 3 4
|
//constructor, operators etc not shown, just conveying the idea
struct CRadius {
double radius = 10.0;
}
|
With this usage:
1 2 3 4 5 6 7 8
|
CRadius TheRadius = 10.0;
//or this
CRadius TheRadius2(10.0);
CArc MyArc(TheRadius, TheCentrePoint); // constructor call
|
Also would like to use the variable directly (as if it was a double):
std::cout << TheRadius;
Might need to overload
operator()
to do this? No arguments means return a value, 1 argument means assign a value.
Now the second case, where the type is an existing class:
typedef class CCentrePoint<CPoint>; //could have a boost class here instead of CPoint
Again, it would have to behave like a
CPoint
. I guess the implementation would just inherit
CCentrePoint
from
CPoint
Now we could do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
typedef class CRadius<double>;
typedef class CAngleStart<double>;
typedef class CAngleEnd<double>;
typedef class CAngle<double>; // CStartAngle + CAngle = CEndAngle
typedef class CPointCentre<CPoint>;
typedef class CArc(CPointCentre Centre, CRadius Radius);<CPoint>;
typedef class CPointBegin<CPoint>;
typedef class CPointEnd<CPoint>;
typedef class CPointMid<CPoint>; //Snap to midpt on CAD system
typedef class CPointOnArc1<CPoint>;
typedef class CPointOnArc2<CPoint>;
typedef class CPointOnArc3<CPoint>;
//constructors
CArc(CPointCentre Centre, CRadius Radius);
CArc(CPointCentre Centre, CPointBegin PtBegin, CAngle Angle);
CArc(CPointBegin PtBegin, CPointEnd PtEnd, CRadius Radius); // disambiguated now compared to line above. Centrepoint determined by order of begin & end always counter clockwise
CArc(CPointCentre Centre, CRadius Radius, CAngleStart AngleStart, CAngleEnd AngleEnd); // disambiguated now compared to line above
CArc(CPointCentre Centre, CRadius Radius, CAngleStart AngleStart, CAngle Angle); // disambiguated now compared to line above
CArc(CPointOnArc1 PtOnArc1, CPointOnArc2 PtOnArc2, CPointOnArc3 PtOnArc3);
// plus others, especially for 3D Arcs
|
Here is a question: Is there another way of disambiguating these arguments?
I guess the implementation of this would be easy enough with template code (although I would probably learn a lot if I had a go).
With the mission critical code Bjarne was talking about, it seems that coders didn't use units (the cause of the problem), and I get the impression things weren't strongly typed either, I am guessing they might have had this scenario (a pedagogic example):
double CalcDistanceToMars(double DistanceFromEarth);
rather than:
CDistanceToMars CalcDistanceToMars(CDistanceFromEarth DistFromEarth);
So, if it was easier to create new types, I am thinking that would be an advantage.
I look forward to all the replies. Cheers
Reference to the Mars project, in the compute less section:
http://www.stroustrup.com/Software-for-infrastructure.pdf