Fix scala's weird casting issue