News for hackers. Stuff that matters.
Re:Please put units in names by syntaxfree on Sunday March 20, 2022 @11:05PM (#30749730)
Overloading the asterisk is always weird because multiplication is expected to be associative etc etc.
Re:Please put units in names by vitus on Sunday March 20, 2022 @11:26PM (#30749864)
I'm not sure I follow.

2 * 3 * time.Second is the same whether you group 2 * (3 * time.Second) or (2 * 3) * time.Second (namely, the implicit grouping under left-associativity).

You wouldn't normally write time.Sleep(time.Second * time.Second) because your units wouldn't work out. (Apparently you can write that in Golang; it's just very sketchy and results in a 30-year sleep.)

Re:Please put units in names by thaumasiotes on Monday March 21, 2022 @12:26AM (#30750257)
But from a mathematical point of view, the relationship between a unit and its coefficient is that you're multiplying them together. Why would it be weird to overload the multiplication operator to represent multiplication?
Re:Please put units in names by Laremere on Sunday March 20, 2022 @11:23PM (#30749856)
There is no operator overloading here. The type Duration is effectively an int64: https://pkg.go.dev/time#Duration
Re:Please put units in names by xyzzy_plugh on Sunday March 20, 2022 @11:08PM (#30749761)
It's not overloaded. It's a unit. You can just type

  time.Sleep(time.Second)
but this reads nicely

  time.Sleep(3 * time.Minute)
Re:Please put units in names by ts4z on Sunday March 20, 2022 @11:08PM (#30749759)
time.Second*1 gets the same value; it's not overloaded. Well, ok, so what actually happens is that time.Second is a time.Duration, and Duration*int yields Duration (and int*Duration yields Duration).

But the value of time.Second is actually a Duration with value 1000000, IIRC -- it's microseconds. It's just the type that's special, and the int handling here is general over a lot of types.

It really is nice in practice.

Re:Please put units in names by ts4z on Monday March 21, 2022 @02:10AM (#30750821)
(As noted elsewhere it's nanoseconds.)
Re:Please put units in names by rwcarlsen on Sunday March 20, 2022 @11:11PM (#30749777)
It's not really overloading. it is associative:

    2*time.Hour+1*time.second == time.Second+time.Hour*2
Re:Please put units in names by wtallis on Sunday March 20, 2022 @11:16PM (#30749812)
I believe that's illustrating commutativity, not associativity.
Re:Please put units in names by thaumasiotes on Monday March 21, 2022 @12:28AM (#30750273)
The illustration is wrong, but the claim is correct; multiplication between units and scalars is just as associative as you'd expect. Multiplying one kilowatt by an hour gives you exactly the same result as multiplying 1 by a kilowatt-hour.