News for hackers. Stuff that matters.
Re:Please put units in names by chrismorgan on Sunday March 20, 2022 @11:18PM (#30749837)
I’m not a Go developer, but I understand that from a type and mathematical theory perspective Go’s time.Duration is extraordinarily awful, because of Go’s simplistic type system.

int64 * Duration → Duration and Duration * int64 → Duration both make sense, but I gather this only works with constants. For other values, I believe Go only gives you Duration * Duration → Duration which is just wrong, wrong, wrong, requiring that one of the two “durations” actually be treated as though unitless, despite being declared as nanoseconds.

In the end, it’s probably still worth it, but it’s a case of Go trying to design in a certain way for ergonomics despite lacking the type system required to do it properly. I have found this to be a very common theme in Go. Also that it’s often still worth it, for they’ve generally chosen their compromises quite well. But I personally don’t like Go very much.

Re:Please put units in names by maerF0x0 on Monday March 21, 2022 @10:46AM (#30754719)
my 2c, I think the issue is simply in the name collision of units and the constants. (ie, "seconds" and time.Seconds) .

In reality most programming languages do not have units what so ever (built into the language, maybe tacked on as a library after the fact). They have int64s, a unitless value that just keeps track of whole numbers of whatever it semantically means to the developer. If we want to truly have units in values then we either need 1st class language support (including syntactical support) or a rich library that doesnt just "put units in [symbols]". One could probably make it happen with a type that keeps track of units

    ```
    type unitedVal struct {
         denomerator int64
         numerator int64 
         denomUnits string
         numerUnits string
    }

    func (united *unitedVal) Multiply(by unitedVal) *unitedVal {

        return &unitedVal{
           numerator: united.numerator*by.numerator,
           denominator: united.denominator*by.denominator,
           denomUnits: united.denomUnits + " * " + by.denomUnits,
           numerUnits: united.numerUnits + " * " + by.numerUnits,
         }
    }
    ```
and then filling out for all the other operations you want to support.
Re:Please put units in names by crdrost on Sunday March 20, 2022 @11:50PM (#30750012)
I mean in their defense I see almost nobody doing this right... Like to do full SI you really need 7 rational numbers, maybe a sort of inline “NaN” equivalent for when you add things with incommensurate units... you might also want display hints for preferred SI prefixes (might just be a dedicated format type), and while you're at it you might as well use a Decimal type instead of doubles, oh and probably these numbers should have an uncertainty in them, so probably you want a modular system where you can mixin units or mixin uncertainty and you can start from a base of doubles or decimals or, hell, you start experimenting with continued fractions...Sigh. The real world is complicated.

Every implementation of units is secretly trying to be Moment.js, basically.

Re:Please put units in names by int_19h on Monday March 21, 2022 @04:57AM (#30751823)
All you need is a language that actually incorporates units of measure into the type system - i.e. you can define units orthogonal to types (including relationships between units), and then you can specify both the unit and the underlying numeric type in a declaration.

https://docs.microsoft.com/en-us/dotnet/fsharp/language-refe...

This can also be pulled off with somewhat less pleasant syntax on top of a sufficiently flexible parametrized type system - e.g. C++ templates.

Re:Please put units in names by wnoise on Monday March 21, 2022 @12:14AM (#30750172)
And yet people actually do manage meaningful unit systems that don't allow this.
Re:Please put units in names by account42 on Monday March 21, 2022 @10:33AM (#30754578)
C++11's std::duration manages just fine without that much scope bloat. operator* for two durations is simpley not defined so will lead to a compile error.
Re:Please put units in names by tfigment on Monday March 21, 2022 @12:18AM (#30750206)
Company i worked for 20 years ago had commercial engineering modelling program that did all physical types correctly and did scaling for user but it was fairly unique in scope and like you say almost all programming languages fall short here and it had its own quirks.
Re:Please put units in names by svnpenn on Sunday March 20, 2022 @11:37PM (#30749929)
All that, and yet I don't see a single example of what would be "correct", or an example language that does it better. This just comes off as poorly thought out rant.

In my opinion, it is well designed. First of all, who is multiplying time.Duration against itself? I've been programming Go for a few years basically every day, and I've only ever seen the package constants used by themselves, or with untyped constant. I think it's a great syntax, better than any example in the article, as you don't have mystery numbers.

Re:Please put units in names by tedunangst on Monday March 21, 2022 @12:42AM (#30750368)
One alternative is rust Duration, which makes you spell out all your arithmetic operations. But this is exactly the approach taken by go time.Time, so the part about the type system being incapable or whatever is kinda misguided. https://doc.rust-lang.org/std/time/struct.Duration.html

C++ chrono::duration allows arithmetic operations with more sensible overloading. https://en.cppreference.com/w/cpp/chrono/duration

Re:Please put units in names by chrismorgan on Monday March 21, 2022 @02:14AM (#30750834)
Once you go beyond constants (which in practice means literals, I think, but I’m not conversant enough in Go to be confident), Go requires that you multiply Duration by Duration—you can’t multiply it with a scalar outside of constants. In other words, as soon as a scalar multiple becomes a parameter rather than a constant, you can’t just do `n * duration`, but have to do something more like `Duration(n) * duration`, which is obviously physically wrong for a system of units, because the unit should be time squared, not time.

As for languages doing it better, approximately every single language that has strong static typing and uses dedicated types for times does it better. Rust is the one I’m most familiar with and comfortable with.

Re:Please put units in names by svnpenn on Monday March 21, 2022 @12:34PM (#30756145)
You seem to be confusing the Duration type with Duration values. Yes, if you insist on using a typed value, instead of an untyped constant, then that value needs to be type Duration.

But Duration(1) is way different than time.Second. honestly it just sounds like you don't know the language, and aren't willing to learn it. Go is not Rust. Things are different, that doesn't mean Go sucks.

Re:Please put units in names by chrismorgan on Monday March 21, 2022 @11:05PM (#30762307)
The entire purpose of the distinction that I’m remarking on is that Duration * Duration → Duration is mathematically utterly incorrect, and especially super misleading when the base quantity for the unit is nanoseconds rather than seconds, yet that is what Go requires, beyond constants, which it special-cases. To be sure, with durations, the distinction doesn’t matter much because arithmetic performed is with constants, and that’s why I say that Go is probably still better with this wonky unit scheme than with entirely unitless quantities, but there are plenty of situations where you will want to multiply durations by typed numbers, and so you’re forced to do the mathematically-ridiculous `Duration(n) * duration` rather than `n * duration`.
Re:Please put units in names by svnpenn on Tuesday March 22, 2022 @12:14AM (#30762672)
I'm sorry, but I just don't think people use code like you're describing. In your mind, you see this:

    hello := 2
    hello * time.Second // oh no
But people actually use code like this:

    hello := time.Second
    hello *=2
People working with Go code (who know what they're doing), don't declare an int, only to immediately cast it to something else.
Re:Please put units in names by chrismorgan on Tuesday March 22, 2022 @05:53AM (#30764322)
You’re looking at this from the application perspective, where with the specific example of durations constant multiplication is certainly far, far more common. But you’re discounting library concerns, where it would not be out of the ordinary to receive a time.Duration and an int64 from parameters or a struct or similar.

This is also pretty typical of the trade-offs Go makes: it focuses on making things nice for the application writer, mostly pretty successfully, but at the regular cost of pain and with serious typing compromises for the library writer.

Re:Please put units in names by bobbylarrybobby on Sunday March 20, 2022 @11:47PM (#30749997)
What is correct is that duration ± duration = duration, duration * scalar = duration, timestamp ± duration = timestamp, timestamp - timestamp = duration, and anything else doesn't compile.
Re:Please put units in names by Groxx on Sunday March 20, 2022 @11:52PM (#30750026)
To call it out specifically: this does not include `duration * duration = duration`

Go is currently allowing that, which makes `delaySecs * time.Second` a billion times larger than it appears to intend. I've personally run across code that has this kind of flaw in it... at least several dozen times. It's the kind of thing that's only noticed when it misbehaves visibly while someone is watching it.

(I read a lot of other-teams' code, which is in various states of quality and disarray)

Re:Please put units in names by chrismorgan on Monday March 21, 2022 @02:16AM (#30750849)
And it’s not just that Go allows that, but that that’s actually the only general way of doing it, as Go only allows duration * scalar in constant context (which is admittedly all most people do with durations, which is why I say it’s still probably better than Go did it this way, given their deliberately limited type system).
Re:Please put units in names by afiori on Monday March 21, 2022 @07:14AM (#30752642)
not being able to sum timestamps is a bit beyond the scope of units, it is more of a vector vs point distinction
Re:Please put units in names by svnpenn on Sunday March 20, 2022 @11:59PM (#30750078)
If you exponent time.Duration, that's a user error.

What you're suggesting would be like a compiler error for multiplying two int64s

Re:Please put units in names by linkdd on Monday March 21, 2022 @12:34AM (#30750312)
What they're suggesting is that `time.Duration` should not be an int64.

  1 second * 1 second = 1 second²
  1 meter * 1 meter = 1 meter²
  1 meter / 1 second = 1 m/s
Those are not user errors, those are physical values. A physical value is two things:

  - a scalar (int64, float, ...)
  - a unit (meter, second, inches, ...)
If the type system of your programming language does not allow you to define units, this should at least be a structure with a scalar and an enum, and functions to cast from one unit to another (if possible).

Working with units is a common thing in science.

Re:Please put units in names by skissane on Monday March 21, 2022 @12:48AM (#30750409)
> A physical value is two things:

I would argue it is actually three things: a scalar, a unit, and an indication of error – which is at least another scalar, but there are multiple ways of expressing error, so it might require more than just a single scalar (such as an interval and the probability the actual value lies within that interval.)

> If the type system of your programming language does not allow you to define units, this should at least be a structure with a scalar and an enum

Ideally more than just an enum – Newton = kg*m*s^-2 (equivalently kg^1*m^1*s^-2) – which suggests a set of pairs (unit and exponent).

Re:Please put units in names by linkdd on Monday March 21, 2022 @01:02AM (#30750485)
Yes, thank you for the precisions, this just make my point stronger: int64/float are very ill-suited to represent such values.

And it's especially true for time units.

For example, "how many seconds is one month?" does not make sense, but "how many seconds is january/february/march?" does make sense. The unit "month" does not really exist, each calendar month is its own unit.

And "february" is not even a "stable" unit because sometimes it's 28 days, sometimes it's 29 days. Even a minute can some rare times be 61 seconds.

This is why in physics, we use seconds multiplied by powers of 10 and nothing else.

To my knowledge, there is not a single programming language that differentiate a "scalar" and a "quantity" (scalar, unit, error).

Re:Please put units in names by conradludgate on Monday March 21, 2022 @03:10AM (#30751113)
It should be fine to multiply two durations, but it should not return the same time.Duration type
Re:Please put units in names by int_19h on Monday March 21, 2022 @05:01AM (#30751842)
> All that, and yet I don't see a single example of what would be "correct", or an example language that does it better.

F#, Rust, C++?..

> First of all, who is multiplying time.Duration against itself

Physicists do, every time they have to deal with acceleration - m/s^2.

Re:Please put units in names by WinterMount223 on Monday March 21, 2022 @07:11AM (#30752620)
You are right however physicists do not multiply duration x duration but time x time. Duration is discrete and time is infinitesimal.
Re:Please put units in names by thwarted on Monday March 21, 2022 @12:36AM (#30750333)
The provided example is pretty rough, and I'm sure it's occurred in the wild. Sure, put the units in the variable name, and it encourages this kind of mistake, because time.Duration is not "seconds", it is a duration. The variable name should match the API of time.Sleep, which takes a duration. The variable should be named delay. A variable named delaySecs is the same kind of maintenance headache as a variable named "two_days_ago = 2.days.ago" in ruby.

The variables used for accepting and parsing input are the ones that should have units in them in this example. Although if you need a delay specified, it's valuable to be explicit and robust in the input and accept a string time.ParseDuration understands. Then you don't have this units problem in your variable naming at all, allows easier input of wider ranges of values by the operator, and makes input validation (if only a subset of durations are allowed) more concise and consistent.

Re:Please put units in names by conradludgate on Monday March 21, 2022 @03:14AM (#30751143)
I've seen a lot of code at my last job in Go where all duration variables included their units. It was amazingly bad Go code (it was built in part of the projects PoC by new Go devs) but it doesn't help that for the most part it did work.

Time was honestly our biggest source of bugs by far. Although adding time.Time ended up being more problematic than durations which were mostly only constructed like that in tests

Re:Please put units in names by xigoi on Monday March 21, 2022 @03:21AM (#30751199)
Re:Please put units in names by manwe150 on Sunday March 20, 2022 @11:43PM (#30749966)
Just contrived examples, like gravity * t^2 to get distance to fall and such, probably