Search code examples
juliasymbolscomparison-operators

Comparison operators for Symbols


In Julia, is it always the case that :a < :b evaluates to true?

More generally, can I rely on the relational operators (ie <, >, ==, <=, >=) behaving for symbols in exactly the same way they do for strings?


Solution

  • Yes. The source code here shows that Julia uses C function strcmp to do the comparison:

    cmp(a::String, b::String) = lexcmp(a.data, b.data)
    cmp(a::Symbol, b::Symbol) = Int(sign(ccall(:strcmp, Int32, (Cstring, Cstring), a, b)))