Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

201–210 of 539 posts

Re: Microfeatures I'd like to see in more languages

#201

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.

Is that giving you a number or a bytestring?

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

#202
post #121
post #77

I want more implicit typing in typed languages. Quite often the compiler knows exactly what type a function will return, but I still need to write it there. Sometimes it’s easy („int“), but what about HashSet >> Typescript does it well. F# (completely statically typed) too…

Nowadays even Java will do that, var list = yourFunctionReturningThatSet();

For Java, I used to write `list = yourFunctionReturningThatSet();` and then have the IDE fill in the type when it complains about the undefined variable.

Re: Microfeatures I'd like to see in more languages

#203
post #200

Earlier quoted context omitted.

I'd gently suggest that if you wanted me to have that context when interpreting your statement, you could have provided it in your original statement or provided it now blamelessly, rather than framing this as a deficiency on my part that I failed to use my crystal ball to determine you were an embedded programmer. (I do very similar things at the application level, for what it's worth.) I believe the solution I prop…

Yes, exactly, now you need special tools (you mentioned IDEs) when previously you could have used grep.

You could also use CScope or grep for the constant's variable name. I don't consider an IDE to be particularly specialized, but you do you, I hate it when people tell me I'm using the wrong IDE, so I'm not going to tell you to use an IDE.

To be clear, I grep for things all the time, even though I use an IDE.

Re: Microfeatures I'd like to see in more languages

#204
post #118
post #110

local const variables like JavaScript has them. You can declare variables with „let“ (mutable) or „const“ (immutable). This is really great when reading code, because you never have to check if some code may change the variable at some point. And you usually declare most variables as const. A lot of languages provide immutable variables only for class members or statics, but not for local variables.

I really like Scala’s `val` vs `var`.

Same in Kotlin

Re: Microfeatures I'd like to see in more languages

#205
post #200

Earlier quoted context omitted.

Yes, exactly, now you need special tools (you mentioned IDEs) when previously you could have used grep.

You could also use CScope or grep for the constant's variable name. I don't consider an IDE to be particularly specialized, but you do you, I hate it when people tell me I'm using the wrong IDE, so I'm not going to tell you to use an IDE. To be clear, I grep for things all the time, even though I use an IDE.

It's not about searching for specific values either. Sometimes you find some unknown value that's not reflected in the out of date or simply wrong datasheet. What does it do? Well, it's quite likely it does something related to other values that use the same mask, so you can try to figure out the mask by searching for lexical subsets of the value, which because of the magic of base 16 is an extremely effective strategy in finding related things. Much harder now with underscores.

And it's also about being able to search Google for a found constant, which indexes other people's code. There have been plenty of occasions where I found newer version of proprietary driver code that the hardware manufacturer claimed it either lost or doesn't exist and won't provide to us by simple searching for constants on Google...

But more often is to just find drivers from other operating systems that already support that device, or mailing lists or forums where other people try to reverse engineer the device.

Re: Microfeatures I'd like to see in more languages

#206

Negative array subscripts. So a[-1] means the last element of an array, a[-2] means the second last, and so on.

As a choice then perhaps but as a default and unalterable behaviour it can be a bloody timewaster when negative subscripts are a runtime error in your work. I've hit that in python and didn't enjoy it.

A nice alternative I've seen is that negative index is an error, but there is special syntax for indexing from the back like array[end], array[end-1], array[end-n], where n is a (positive) variable. Likewise, end can be used in range definitions like array[5:end]. Julia and Matlab both have this.

Re: Microfeatures I'd like to see in more languages

#207

Php supports kebab-case variables: ${"variable-name"}=123; Isnt it beautiful?

I agree that kebab variables aren't to my taste either, but I am partial to the notion of kebab-case keywords that I encountered in a JEP draft [0]. It suggests expanding the keyword vocabulary with a form that is otherwise invalid syntax, similar to how java treats module-info.java and package-info.java as valid files, but rejects any other hyphenated java class filename.

[0] https://openjdk.org/jeps/8223002

Re: Microfeatures I'd like to see in more languages

#209
post #79

Earlier quoted context omitted.

Any language that supports overriding the index operation should support this. You should be able to do this in C# with a struct with a backing array, for instance. If you're going to do this, use the word "Circular" in it, and I would also insist that if a has 4 elements, then a[0] == a[4] == a[8]. In other words, you always just take the (positive) index modulo the size of the area. Then a[-1] is the same as a[N-1]…

You'd have to care about the difference between indexing with a literal and indexing with variables of different types/widths, and how indexing with a variable interacts with the size of the area. For instance if you have an int array that contains the numbers 1-250 and you index with a uint8 variable i, for (uint8 i = 247; i++;) { // print circ_arr[i] } for the values of i near the overflow points of the circular ar…

Right, the calling code needs to handle its own integer overflows, of course. And if your circular array has a size other than a power of 2 you can get only a partial enumeration in the cycle that includes overflow. Sure. But are you really indexing an array of unknown size with a uint8? It's really impossible that there might be more than 255 things you ever care about? No. Everyone who is using a uint8 to index arrays is either doing something extremely low-level and fiddly where abstractions like this simply don't apply, or they're idiots who are doing cargo-cult shotgun "optimization" because they don't know how to write code that works.

If you're indexing with a [u]int32 you need to worry about this once every 4 billion increments, and if an incomplete cycle is a show-stopper for you, you can compute a safe modulo yourself based on the size(s) of your circular array(s), but more likely you just need something else. But really, you don't care if your cache hiccups a little once every 4 billion caches.

You make a good point, of course, I'm just allergic to people poking holes in back-of-the-napkin explanations of things with the trite "but integers can overflow!" It's one of those most common well actuallys written on this site. Of course integers can overflow. They almost never do though, do they? And if they do, a test fails and you add a single line somewhere to fix it.

I really think the vast majority of programmers are too often thinking about bits when they should be thinking about math.

Re: Microfeatures I'd like to see in more languages

#210
post #15

> Instead of writing 10000500, you can write 10_000_500, or 1_00_00_500 if you’re Indian. I hate this so much. It means I can't grep for a constant.

When do you find yourself grepping for a constant (as opposed to it's name)?

I explained down in this thread.
Post reply on HN