Chain two different struct specialization

bearophile bearophileHUGS at lycos.com
Fri Mar 1 07:04:59 PST 2013


Andrea Fontana:

> How to put s1,s2,3... in a range/array or something 
> similar/iterable? Probably there's no way (inside variant?)...

One solution is to not use templates:



immutable struct Weights {
      double foo, bar;
}

enum Weights firstWeights  = { foo: 0.3, bar: 0.4 },
               secondWeights = { foo: 0.5, bar: 0.2 };

struct MyStruct {
	int prop, prop2;
      immutable Weights weights;

	this ( Weights weights_, in int p, in int p2) pure nothrow {
          prop = p;
          prop2 = p2;
      }
}

double likeness(in ref MyStruct first, in ref MyStruct second) {
	double v = (first.prop - second.prop) * first.weights.foo *
second.weights.foo;
	return v + (first.prop2 - second.prop2) * first.weights.bar *
second.weights.bar;
}

void main() {
	immutable s1 = MyStruct(firstWeights,  10,  8);
	immutable s2 = MyStruct(firstWeights,   9, 10);
	immutable s3 = MyStruct(secondWeights,  9, 10);

      import std.stdio;
	writeln(likeness(s1, s2));
	writeln(likeness(s1, s3));

      const r = [s1, s2, s3];
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list