Search code examples
ethereum

cryptozombies why use keccak in comparing


I am already in lesson 2 Chapter 13 of cryptozombies tutorial. In feedAndMultiply function why they use keccak to compare Kitty rather the exact character.

if (keccak256(_species) == keccak256("kitty"))   

why not

if (_species == "kitty") ?

Solution

  • The == operator is only supported for booleans, integers, fixed point numbers (once support for fixed point numbers is launched), addresses, and static byte arrays. Solidity does not support == for dynamic arrays (which string is). To work around this, they are simply taking the hash value of the two strings and then comparing the result.

    The Types page in the Solidity docs outlines all of the supported operations for each type.