Comment

Comment by justinsaccount

I'm not sure if type aliasing is for that use case. I think you're more thinking about things like

  type Celsius int
that in theory prevent you from passing a bare int and mixing up Celsius/Fahrenheit.

In go this almost works. If you define

  type Celsius int

  func isBoiling(t Celsius) bool {
      return t >= 100
  }
then You need to call it by doing something like

  t := Celsius(100)
  isBoiling(t)
and just

  t := 212
  isBoiling(t)
will not compile with

  cannot use t (type int) as type Celsius in argument to isBoiling

But unfortunately you can get away with

  isBoiling(212)

However, If you just used a type alias then you'd be able to do either.

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton