thanos icon indicating copy to clipboard operation
thanos copied to clipboard

Add special casing for `has_key?`

Open redneckbeard opened this issue 3 years ago • 0 comments

Currently has_key? gets treated like any other method, which means that this Ruby:

if h.has_key?(:foo) 
  puts "has key foo"
end

gets compiled to this Go:

var hasKey bool
if _, ok := h["foo"]; ok {
  hasKey = true
}
if hasKey {
  fmt.Println("has key foo")
}

which is less than ideal, since the idiomatic key presence test would be simply

if _, ok := h["foo"]; ok {
  fmt.Println("has key foo")
}

Instead, the TransformAST func for has_key? should return a ast.BadExpr and rely on special handling by method name in the compiler.

redneckbeard avatar Feb 21 '22 02:02 redneckbeard