Troubles with template constraints on string and static if
sungal
francesco.galla at edu.unito.it
Thu Apr 26 15:06:49 UTC 2018
I have this piece of code and I can't understand why the `static
if` conditionals are always false.
```
import std.digest.sha;
import std.file;
import std.stdio;
void main()
{
auto hash1 = produceHash!string("prova.d");
auto hash2 = produceHash!File(File("./prova.d"));
}
string produceHash(Type)(Type data)
if(is(Type == string) || is(Type == File))
{
string h;
static if(is(Type == File)) {
enforce(data.exists, "File does not exist.");
// Hashing both name and file, to avoid having the same
Hash on empty files
return
toHexString(digest!SHA256(data.byChunk(4096*1024))).dup;
} else static if(is(Type == string)){
return toHexString(digest!SHA256(data)).dup;
}
static assert(false, "Data must be string or file. ");
}
```
```
prova.d(22): Error: static assert "Data must be string or file. "
prova.d(7): instantiated from here: produceHash!string
```
More information about the Digitalmars-d-learn
mailing list