byte + byte = int: why?

2019-01-18 Thread Mek101 via Digitalmars-d-learn
I have the following line of code: temp[fowardI] = temp[fowardI] + temp[backwardI]; Where temp is a byte array (byte[]). When I try to compile it dmd gives me this error: source/hash.d(11,25): Error: cannot implicitly convert expression `cast(int)temp[fowardI] + cast(int)temp[backwar

Re: byte + byte = int: why?

2019-01-18 Thread Mek101 via Digitalmars-d-learn
On Friday, 18 January 2019 at 17:15:09 UTC, Steven Schveighoffer wrote: What is 127 + 127? Answer: 254. Which if converted to a byte is -127. Not what you might expect if you are doing addition. Quite similar to int.max + int.max In fact, D promotes all integral types smaller than int to int

Problems instantiating template class

2019-04-06 Thread Mek101 via Digitalmars-d-learn
I'm rewriting from C# a small library of mine to practice with D. I have a class: class WeightedRandom(T, W = float) if(isNumeric!W) { // Fields private W[T] _pairs; // The total sum of all the weights; private W _probabilities; /// Code... } An

Re: Problems instantiating template class

2019-04-06 Thread Mek101 via Digitalmars-d-learn
On Saturday, 6 April 2019 at 17:44:25 UTC, Nicholas Wilson wrote: Hmm, import std.traits; class WeightedRandom(T, W = float) if(isNumeric!W) { // Fields private W[T] _pairs; // The total sum of all the weights; private W _probabilities; /// Code...

Re: Problems instantiating template class

2019-04-06 Thread Mek101 via Digitalmars-d-learn
On Saturday, 6 April 2019 at 18:08:31 UTC, Mek101 wrote: On Saturday, 6 April 2019 at 17:44:25 UTC, Nicholas Wilson wrote: Hmm, import std.traits; class WeightedRandom(T, W = float) if(isNumeric!W) { // Fields private W[T] _pairs; // The total sum of all the weights;

How to "Inherit" the attributes from a given callable argument?

2019-06-12 Thread Mek101 via Digitalmars-d-learn
I'll try to be straight. I have the following function: public size_t indexOf(alias pred = "a == b", Range)(Range array) { alias predicate = unaryFun!pred; for(size_t i = 0; i < array.length; i++) if(predicate(array[i])) return i;

Re: How to "Inherit" the attributes from a given callable argument?

2019-06-12 Thread Mek101 via Digitalmars-d-learn
I didn't know it applied to templates other than lambdas. Thank you for your explanation.

Test the return type of 'unaryFun' and 'binaryFun'.

2019-08-29 Thread Mek101 via Digitalmars-d-learn
As the title says, is there a way to test the return type of the 'unaryFun' and 'binaryFun' templates from 'std.functional'? I have the following code, and I want to to be sure that 'predicate' returns a boolean, but neither 'is(typeof(predicate) == bool)' or 'is(ReturnType!predicate == bool)'

Re: Test the return type of 'unaryFun' and 'binaryFun'.

2019-08-29 Thread Mek101 via Digitalmars-d-learn
I made the following enumerations to test the predicates. private enum bool isUnaryPredicate(alias pred, Range) = is(typeof(unaryFun!(pred)((ElementType!Range).init)) == bool); private enum bool isBinaryPredicate(alias pred, Range, V) = is(typeof(binaryFun!(pred)((ElementType!Range).init, V.ini