Do we need to use the same integer types in operations?

Hi again!

For example, take this code: Playground

class Player
  property flask_charges = 3_i8
end

p = Player.new

p.flask_charges -= 1
p.flask_charges -= 1_i8

puts p.flask_charges
puts typeof(p.flask_charges)

Some questions:

  • What’s the difference between line 7 and 8?
  • It seems to be using 1_i8 for the decrement operation, however, is there any performance benefit in writing 1_i8 out, as compared to just 1?

I was pondering this dilemma all day because I just noticed _i8 littered all over, and honestly, removing all the _i8 suffixes makes it look less verbose / easier to read. Not only that, but I keep thinking of the TYPES! Types need to match! If I set a property to a specific type, I always think I should use the same kind of type modifying it. For example, typeof(1) is Int32. Int32 is not an Int8. So it kinda feels weird to modify a Int8 with a Int32.

Or, am I just overthinking it?

1 Like

There’s a recent and undocumented feature of the language that lets you pass a literal number type to a method that is restricted by a different number type. It only works with literals like 1, 123, etc., never with variables. In your example the 1 is implicitly converted to 1_i8, so the two lines are the same, except that the first one is easier to read and write.

4 Likes

Alright sweet! Well, RIP to my _i8s then! Bye felicia :joy:
edit: Thanks for the quick reply!

1 Like