What are some syntaxes programming¹ languages have used for units?

I have a human-editable format where a human writes numbers, but might want to specify "3" vs "3 hz" vs "3 samples". I want units to be string names, however

- Allowing the number to be separated from the unit by a space makes my parser hard
- Forcing them together, like 3hz, creates other problems, like hex support

¹ Would also be happy for syntaxes "not normatively programming" languages have used, like spreadsheet programs

Follow

@mcc Would a DSL be helpful here? It seems like the space between a number and a unit would fit, but i know too little about your other requirements.

@mcc My knowledge is very, very dated (like 10 years ago), but scala worked pretty nicely back then. Ofc the fan club can be a bit unpleasant.

@mcc Ah, just now getting to your previous toots... masto is a bit problematic here, scroll on and loose the original toot, or answer and possibly miss context. Sorry for the chaotic answer ^^

@admitsWrongIfProven if you want the full context, which there's a lot of but no reason you'd have seen it

mastodon.social/@mcc/113556420 and preceding posts and preceding linked posts… you don't have to read this, there's a lot :)

An example of a program in the current language is

(P t--3 0 p8x p13x p0x p8x p13x)
(R t--2 0 t--3 p0 x p8 x p13 x)
R P -2 R P +4 R P -2

So as you can see there is a high priority on terseness. But in some unusual circumstances, I might want to get a little more verbose.

@admitsWrongIfProven So I'm looking for something that preserves the potential for terseness in the general case while allowing the potential for specificity in edge cases. In the above sample, "p" and "t" use two different unit types, but both "p" and "t" have multiple unit types they *could* use, and which in some specific circumstances, I might want to use.

But also because of the compactness of the language, a space followed by an identifier is hard to parse. Most commands are one letter.

@mcc Hmm, sounds like your actual requirement demands more than one letter. Ofc you could use pPqQ... to give p multiple unit types, but i'd say that won't be readable. Is there anything bad about "<p/t><unittype1/2/3>" as a two letter abbreviation? Compact is good, but if you'd loose necessary information not possible, right?

@admitsWrongIfProven so my thinking is that
- normally each letter has a meaning by itself, so multi-letter symbols are unusable
- but if i put a symbol in between, such as !, >, :, /, or _, then the parser can be aware that everything after the "unit next" symbol and before the space is a unit, and therefore should be treated as multi letter

@mcc Ah, i think i'm starting to get it...
p -> has a default meaning
p_<x> -> the <x> is an unusual unit

Squashing those together without "_" has problems, so the "_" makes sure you don't have problems with spaces as seperators but parse the whole expression at once.

Hmm, i guess you already solved it ^^
I might not have contributed, but it was interesting :-)

@admitsWrongIfProven yeah, i already had a pretty clear idea of how *I* was going to proceed when I asked the question :)

but what I was curious about was how *other* languages had solved this, to give me ideas on "probably I put a symbol to indicate a unit— has anyone done this before, what symbol?"

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.