Initialising a class with a native datatype necessary for GMP

Matthias Walter walter at mail.math.uni-magdeburg.de
Tue Nov 27 03:16:42 PST 2007


In this post I will try to explain a problem, which I encountered in my GMP implementation. There are different approaches, how one may implement GMP, but the main goal here is that GMP numbers (mpz, mpq, mpf for integer, rational, floating point) can be used like normal ints and floats!

Especially, the following things should work (with T any of those types):

| T func (T a, T b = 0)
| {
|   T result = 10;
|   return result * a + b;
| }
|
| ...
|
| int main (char[][] args)
| {
|   T value = 2;
|   value = func (value, 1);
|   value = func (value);
|   Stdout.format ("value = {0}", value).newline;
| }

There are two things, which are AFAIK impossible to do with current D:
 (1) T can be a default parameter like in line 1.
 (2) T can be initialized with a type that is not T (e.g. mpz with an int)

Although, there are at least two different ways to implement GMP: either classes (which is very elegant in D) or templated structs (like in C++), but both cannot solve issues (1) or (2)!

With current D, the only solution, I found, was the following helper function:

| T num (T) (int n)
| {
|   static if (is (typeof(T) == mpz))
|   {
|     return new mpz (n);
|   }
|   else
|   {
|     return n;
|   }
|}

With this template, one can write

| T func (T a, T b = num !(T) (0))
| {
|   T result = num !(T) (10);
|   ...
| }

But this is ugly code and I'm lazy to add all those calls when refactoring an int-based program to a template/GMP-based one!

I have the following ideas, how one could make this more pretty:

1. Make an implicit cast from a type to a class equivalent to a constructor, taking the type as the only parameter.

2. Let the expression "new int (10)" be semantically the same as "10" (Although I think this would be very inconsistent behavior)

Any other ideas?

I believe that there is the necessarity for something to happen, because GMP is a very important choice for computing in science and the current status seems to dissatisfactory to me!

best regards
Matthias Walter



More information about the Digitalmars-d mailing list