Search code examples
crystal-lang

Empty Set literal syntax


Looking for the right syntax or if there's a bug/rough edge:

Set{}

for empty hashes use '{} of KeyType => ValueType' <-wrong

Set{} of Char

expecting token '=>', not 'EOF' <-wrong

Set(Char){}

for empty hashes use '{} of KeyType => ValueType' <-wrong

Set(Char).new # not a literal

Set{'a'} # not an empty Set literal

Solution

  • There isn't one, use Set(Char).new.

    Set literals, such as Set{'a'} actually compile to:

    __tmp_var = Set(typeof('a')).new
    __tmp_var << 'a'
    __tmp_var
    

    so there's no performance benefit to using an empty literal instead of Set(Char).new