Chain two different struct specialization
Andrea Fontana
nospam at example.com
Fri Mar 1 08:25:02 PST 2013
On Friday, 1 March 2013 at 16:03:58 UTC, bearophile wrote:
> Andrea Fontana:
>
>> BTW, compiler can't guess s1 and s2 weights, should it?
>>
>> if inside likeness() i write:
>>
>> enum test = first.weights.foo * second.weights.foo;
>>
>> it said that can't read first and second value at compile time.
>
> firstWeights and secondWeights are compile-time constants, but
> the arguments you give to likeness are run-time values, so they
> are unknown at compile-time inside likeness.
>
> There are ways to solve that problem, but I don't know how much
> good this is:
>
>
> 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 {
> this.weights = weights_;
> this.prop = p;
> this.prop2 = p2;
> }
> }
>
> double likeness(alias first, alias second)() {
> enum test = first.weights.foo * second.weights.foo;
> 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() {
> enum s1 = MyStruct(firstWeights, 10, 8);
> enum s2 = MyStruct(firstWeights, 9, 10);
> enum s3 = MyStruct(secondWeights, 9, 10);
>
> import std.stdio;
> writeln(likeness!(s1, s2)());
> writeln(likeness!(s1, s3)());
>
> const r = [s1, s2, s3];
> }
>
> Bye,
> bearophile
but:
enum s1 = MyStruct(firstWeights, 10, 8);
enum s2 = MyStruct(firstWeights, 9, 10);
writeln(likeness(s1, s2));
still gives error: and s1 and s2 are known at compile time,
aren't them?
Your solution will create code for each item couple, isn't it?
Not good in my case :) Other ideas?
More information about the Digitalmars-d-learn
mailing list