Weird error when compiling.
Agustin
agustin.l.alvarez at hotmail.com
Sun Oct 20 15:04:48 PDT 2013
On Sunday, 20 October 2013 at 21:54:37 UTC, Agustin wrote:
> On Sunday, 20 October 2013 at 02:14:55 UTC, TheFlyingFiddle
> wrote:
>>> Anyway to evaluate the name of the class and return its hash
>>> at compile time?
>>
>> I couldn't find the built in hash unction for strings so i
>> used the one from http://dlang.org/hash-map.html as an
>> example. More advanced and better hash functions can be found
>> online.
>>
>> public class ComponentDetail(T) : Component {
>> ////////////////////////////////////////////////////////////
>> /// Component's ID as static member.
>> public static hash_t ID = hash!(T);
>> }
>>
>> hash_t hash(T)()
>> {
>> hash_t hash;
>> foreach (char c; T.stringof)
>> hash = (hash * 9) + c;
>> return hash;
>> }
>
> Works perfectly :), now if i want to implement the same but
> rather do something like
>
> template GetHash(string character, hash_t hash = 0, size_t index
> = 0) {
> static if (character[index])
> enum GetHash = GetHash(character, hash = (hash * 9) +
> character[index], ++index);
> else
> enum GetHash = hash;
> }
>
> How should i implement it?
I came up with something like
template GetHash(string character, hash_t hash = 0, size_t index
= 0) {
static if (index < character.length)
enum GetHash = GetHash!(character, (hash * 9) +
character[index], index + 1);
else
enum GetHash = hash;
}
More information about the Digitalmars-d-learn
mailing list