On Mon, 4 Apr 2022 at 12:42, Ricky Teachey <ri...@teachey.org> wrote: > > On Sun, Apr 3, 2022, 6:27 PM Chris Angelico <ros...@gmail.com> wrote: > > On Mon, 4 Apr 2022 at 07:45, Ethan Furman <et...@stoneleaf.us> wrote: > > Mechanically, are `lbs`, `km`, `hr`, etc., something that is imported, or > > are they tags attached to the numbers? If > > attached to the numbers, memory size would increase and performance might > > decrease -- but, how often do we have a number > > that is truly without a unit? > > > > How old are you? 35 years > > How much do you weigh? 300 kg > > What temperature do you cook bread at? 350 F > > > > Very frequently - it'd be called an index. (What sort of numbers > should enumerate() return, for instance? Clearly that, whatever it is, > is an index.) But if every int and float has a tag attached to it, > it's not that big a deal to have either a default tag, or leave the > field NULL or None, to define it to be a unitless value or index. > > ChrisA > > > How much memory would have to be added to every number? Could it be > dynamically sized so it doesn't have a large impact on all the code out there > with no units? But also grow to be big enough to capture the vast > constellation of units and unit combinations out there? >
I'm not sure that the semantics should be defined by the language to that extent. My thinking here is that most of the actual work should be done by a unit-specific library, and the language simply defines some low-level semantics (and, of course, the corresponding syntax). So the effect would be that int and float don't actually change, but numbers-with-units are all instances of custom classes. > I'm unsure simple tags are enough. What should the behavior of this be? > > height = 5ft + 4.5in My view on this is that it should basically be defined as: height = ft(5) + in(4.5) where you register your constructor functions (possibly types, possibly factory functions) using a hook in the sys module. > Surely we ought to be able to add these values. But what should the resulting > tag be? Should we be able to write it like this? > > height = 5ft 4.5in That, I would say, no. There are a lot of human-readable notations that aren't supported by programming languages, such as algebraic "abuttal means multiplication" and such. It's not that hard to say "5ft + 4.5in", just like you'd say "3 + 4j" for a complex number. (Also, I don't really see a lot of point in making feet-and-inches minorly less complicated, given that people should be trying to use metric anyway.) > I was cheerleading this effort earlier and I still think it would be a > massive contribution to needs of the engineering world to solve this problem > at the language level. But boy howdy is it a tough but of a problem to crack. > This is something where (again, my view only, others may disagree) the language itself need not actually make a decision. Ideally, the standard library should have something usable, but it should be such that you could choose to do something else. The standard library could even have more than one unit system in it - for instance, you could have a metric-focused system that converts "5ft" into "1.524m", or you could have a unit-retaining system that keeps everything in the unit specified until such time as conversions are needed, etc. One very important feature for the standard library would be a generic "Quantity" type, which could save a lot of hassle. You could create a "length" quantity and register a number of units, and define that any length can be added to any other length, a length can be multiplied by a scalar, etc, etc, etc. Then a bit of generic handling could also recognize that when you multiply or divide two quantities, you get something in a combined unit (whether that's "length*length" meaning area, or "length/time" meaning velocity), and libraries could slot in whatever units make the most sense. Maybe you're working with astronomical distances, and the meter just causes too many floating-point roundoff errors, so you define your base unit to be the parsec, and everything else derives from that. Or maybe you're working with video game analysis and the most important quantity is "items per minute" (eg 780 iron ore per minute), so you define each type of item as its own quantity, and redefine time with a basis of minutes instead of seconds. Having the tools to do that is probably more important than defining it all at the language level. ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/JXGMAB5W3JNBNPKR7JI5QOV57BLBOQIX/ Code of Conduct: http://python.org/psf/codeofconduct/