News for hackers. Stuff that matters.
Re:Microfeatures I'd like to see in more languages by nicoburns on Thursday January 05, 2023 @04:58PM (#34267415)
One of the best truly micro features I've seen recently (can't remember which langauge unfortunately - it wasn't a mainstream one), is general binary literal syntax of the form:

    0x[de ad be ef 00]
So much nicer than the usual condensed format. And I think it'd be valid syntax in any language that allows binary integer literal.
Re:Microfeatures I'd like to see in more languages by andix on Thursday January 05, 2023 @05:04PM (#34267512)
A lot of languages allow underscores in numeric literals. Something like 10_000. You can put them anywhere and they get ignored. I don’t know if they also allow it for hex numbers.
Re:Microfeatures I'd like to see in more languages by masklinn on Thursday January 05, 2023 @05:12PM (#34267591)
> I don’t know if they also allow it for hex numbers.

They do e.g. Python

    >>> 0x_ab_cd_01_23
    2882339107
or

    >>> 0b_0010_0100
    36
Re:Microfeatures I'd like to see in more languages by rwmj on Thursday January 05, 2023 @05:17PM (#34267665)
Even C these days, although they chose ' instead of _ :-(
Re:Microfeatures I'd like to see in more languages by kwhitefoot on Friday January 06, 2023 @03:58AM (#34272547)
My guess is that they did that because there are real human languages where ' is used as a thousands separator.
Re:Microfeatures I'd like to see in more languages by xigoi on Friday January 06, 2023 @05:16AM (#34273035)
There are also real human languages where a space is used as a thousands separator, and an underscore is kind of the programming equivalent of a space.
Re:Microfeatures I'd like to see in more languages by kwhitefoot on Friday January 06, 2023 @06:03AM (#34273379)
A half space is British Standard for thousands separator if memory serves, also standard in Norway.
Re:Microfeatures I'd like to see in more languages by rwmj on Friday January 06, 2023 @09:18AM (#34274786)
The reason they used quote is outlined in the proposal: https://open-std.org/JTC1/SC22/WG14/www/docs/n2626.pdf

TL;DR is that it's because C++ uses quote. The reason C++ uses quote (they considered underscore) is because of a very obscure feature of C++ called custom literal suffixes which I'd never even heard of, but numbers can be suffixed with a custom identifier, and since single underscore is a valid identifier you can't use that. (https://en.cppreference.com/w/cpp/language/user_literal)

Re:Microfeatures I'd like to see in more languages by andix on Friday January 06, 2023 @03:02PM (#34280183)
Swiss German for example. But space, half space and dot is also common in Europe. Just not a comma, because this is used as a decimal separator (fractions) in most countries.
Re:Microfeatures I'd like to see in more languages by dan-robertson on Thursday January 05, 2023 @05:33PM (#34267856)
Is that giving you a number or a bytestring?
Re:Microfeatures I'd like to see in more languages by nicoburns on Thursday January 05, 2023 @06:15PM (#34268364)
A bytestring I think. Although in statically typed languages there's no reason why it couldn't be both depending on the inferred type.
Re:Microfeatures I'd like to see in more languages by jbverschoor on Thursday January 05, 2023 @05:07PM (#34267544)
ohhh that's cool.. in Ruby you can do 0x_dead_beef_00 or any other combo
Re:Microfeatures I'd like to see in more languages by mncharity on Thursday January 05, 2023 @05:54PM (#34268133)
I like that Elixir bitstrings[1] don't have to be bytes. <<2:3>> is three bits 010.

[1] https://elixir-lang.org/getting-started/binaries-strings-and...

Re:Microfeatures I'd like to see in more languages by WalterBright on Thursday January 05, 2023 @11:58PM (#34271152)
D's approach would be:

    0xde_ad_be_ef_00
Re:Microfeatures I'd like to see in more languages by makapuf on Friday January 06, 2023 @02:02AM (#34271800)
Wouldn't endianness interfere with this notation and the preceding one?
Re:Microfeatures I'd like to see in more languages by unsafecast on Friday January 06, 2023 @08:06AM (#34274187)
Of course it does - endianness interferes with any sort of numeric literal. Doesn't matter if it's hex, decimal, underscored, whatever.
Re:Microfeatures I'd like to see in more languages by Wildgoose on Friday January 06, 2023 @08:48AM (#34274496)
I think you are missing the point - there is no endianness in normal written text, (or code), but there is endianness when that is translated to an actual number - what order are those bytes intended to be used in?

(Given they have been listed separately rather than as a single number).

Re:Microfeatures I'd like to see in more languages by unsafecast on Friday January 06, 2023 @09:34AM (#34274992)
What order are the bytes in 0xabcd intended to be used in? I don't see how making it 0x[ab, cd] would be ambiguous, it's the same assumption of reading left to right.
Re:Microfeatures I'd like to see in more languages by cpurdy on Saturday January 07, 2023 @08:05PM (#34294907)
It's an interesting mix of "hex" and "array" into one syntactic composition. I do kind-of like it.

Here's how we ended up supporting byte strings (# prefix) in Ecstasy, in this case multi-line:

    Byte[] bytes = #|12 34 56
                    |78 9a BC
                    |dE f0
                   ;

    console.println($|bytes=
                     |{bytes.toHexDump(4)}
                   );
Which prints:

  bytes=
  00: 12 34 56 78 .4Vx
  04: 9A BC DE F0 .¼Þð