Joseph Wakeling wrote:

>> - opCall() of AvgWeighted was abstract.
>> - keep in mind that in D classes are CamelCase;
>> - variable names are written like weightSum (but once in a while a underscore
> doesn't kill).
>
> I think it's obvious from my syntax that my background is with C; I'm not
> experienced with Java, C# etc. This may explain some of the problems I'm having.
>
> Regarding opCall I was following the syntax described here:
> http://www.digitalmars.com/d/2.0/operatoroverloading.html#FunctionCall
>
> ... but clearly without understanding it properly.

I have experience with C++ and still don't understand why opCall exists. :) I think I heard that opCall was needed to create struct objects before structs had constructors in D.

Now structs do have constructors, which sometimes conflict with opCall. :)

> What I was aiming for was a bit smartarse -- to have a class which could in some
> cases be treated as a function.

I consider myself a function-happy programmer. To me, not everything is a class. :)

> Each of these classes (later ones will be more
> sophisticated) is meant to be a data analysis tool which takes a dataset of > user-object ratings and user and object reputation values and helps aggregate the
> ratings and in the process update the reputation values.
>
> The aim was that if you just wanted a once-off analysis you could use the class in
> a throwaway fashion -- hence the use of,
>
>    avg_weighted(......);

It could be a function that instantiates on object, that would be thrown away.

> It's maybe not the best way to approach what I want to do, but since D is a new > language for me, I thought I would be playful with it and try and bend it around
> in some interesting ways.

No harm in that. :)

>> - Be careful because ref arguments are tricky.
>
> The choice is deliberate here, because the arrays passed to the constructor (or
> opCall) are meant to be modified.

D has "reference types". When you pass a class object to a function "by-value", it is actually passed-by-reference. I think this is the same in Java.

You can imagine the function parameter being a pointer behind the scenes.

ClassType variable = new ClassType;
ClassType variable2 = variable;

You have a single "object" created with new, and two "variables" that refer to that object.

>> - There is a line like foreach (r; reputationUser) r = 1; that can be a bug.
>
> I guess that I should put a 'double' in front of the r, no? In any case, I guess
> there is a better way of setting all elements of an array equal to 1.0.

You would put 'ref' in front of the foreach variables. Otherwise they are copies in the foreach loop.

>> - foreach (objectID, rating; reputationObject) rating /= weightSum[objectID];
> can be another bug.
>
> ... so should be uint objectID, double rating ... ?

Same: Should probably be 'ref rating' if you want to modify reputationObject.

> I think it's obvious that I want each the value of each element of
> reputationObject to be divided by the value of the corresponding element of
> weightSum -- is there a more intelligent way of doing this?

>> - I suggest to add contracts and unittests.
>
> As you might have guessed, I'm not a developer -- can you provide more info?

They are of the greater features of D. :) You can define function pre- and post-conditions and struct and class invariants. You can have unittest blocks... Great stuff! :)

http://digitalmars.com/d/2.0/unittest.html

http://digitalmars.com/d/2.0/dbc.html

http://digitalmars.com/d/2.0/class.html#Invariant

Ali

Reply via email to