Int as parameter type

What is the difference between using Int as a parameter type instead of e.g. Int32 or UInt32? Does it just make it possible to use types that are subclasses of Int as arguments?

def add(a : Int, b : Int)
  return a + b
end
def add(a : Int32, b : Int32)
  return a + b
end

Precisely. That way, you can receive any type of Int, regardless of bit length or signed-ness. Sometimes you’ll want to scope that to Int::Signed or Int::Unsigned if restricting based on signed-ness is important, but often it’s not super important.

Using a wider type like Int (or the slightly more specific signed/unsigned variants) has mostly been useful when I’m likely to receive either a numeric literal (defaulting to Int32) or a number that comes back from Redis or Postgres (likely to be Int64). I don’t generally use Int8 or Int16 all that much, but just having 32/64-bit flexibility does a lot.

2 Likes

I see. I just wasn’t sure if there was more to it or if it just made it possible to use subclasses.

Thank you for the answer. It was super helpful! :slight_smile:

1 Like

Int also includes BigInt, if you have that required. (BigInt is neither Int::Signed nor Int::Unsigned.)

1 Like