www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do you call hashOf() on a string?

reply Enjoys Math <enjoysmath gmail.com> writes:
module smallest_grammar;
import std.conv;
import std.algorithm;

struct Symbol(T) {
public:
	this(T sym, bool isVar) {
		this.sym = sym;
		this.is_var = isVar;
	}

	 property T symbol() { return sym; }
	 property bool isVar() { return is_var; }


private:
	T sym;
	bool is_var = false;
}


struct String(T) {
public:
	this(const T[] s) {
		this.s = s.dup;
	}
	
	alias s this;

	string toString() const { return to!string(s); }
	string flattened() const { string t = "";  foreach (c; s)	t ~= 
to!string(c);  return t; }

	size_t toHash() const  safe pure nothrow
     {
		return flattened().hashOf();
     }

     bool opEquals(ref const String t) const  safe pure nothrow
     {
		if (t.length != this.length)
			return false;

		for(size_t k=0; k < t.length; k++)
			if (t.s[k] != this.s[k]) return false;

		return true;
     }


private
	T[] s;
}

-----


My hash function:

	size_t toHash() const  safe pure nothrow
     {
		return flattened().hashOf();
     }

-----

Is yielding:
smallest_grammar.d(35): Error:  safe function 
'smallest_grammar.String!string.String.toHash' cannot call 
 system function 'object.hashOf!string.hashOf'
May 22
parent Enjoys Math <enjoysmath gmail.com> writes:
Changing  safe to  system worked.  IDK, but w/e! ;-)
May 22