How do you call hashOf() on a string?

Enjoys Math via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 22 12:48:07 PDT 2017


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'




More information about the Digitalmars-d-learn mailing list