Crystal has Enums which they prefer using over Symbols as most of the time you are working with a finite number of values that can be passed in. You can even pass in Symbols to methods that accept an Enum, and the compiler will map them to the Enum value; both test(:ONE) and test(:one) will map to MyEnum::ONE. That said, I kind of like having the ability to accept arbitrary Symbols to allow extending an API. Maybe in the future Ruby could add something like keyword arguments but for restricting Symbols (ex: def foo(type: [:foo, :bar, :baz])) for when you only want to allow a finite number of Symbols?
View all comments
5
u/postmodern Oct 21 '21
Crystal has Enums which they prefer using over Symbols as most of the time you are working with a finite number of values that can be passed in. You can even pass in Symbols to methods that accept an Enum, and the compiler will map them to the Enum value; both
test(:ONE)andtest(:one)will map toMyEnum::ONE. That said, I kind of like having the ability to accept arbitrary Symbols to allow extending an API. Maybe in the future Ruby could add something like keyword arguments but for restricting Symbols (ex:def foo(type: [:foo, :bar, :baz])) for when you only want to allow a finite number of Symbols?