Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

211–220 of 539 posts

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

#211
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…

Why is an overflow that is generated by the caller your concern?

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

#212
post #205

Earlier quoted context omitted.

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 strate…

For sure. I think there are some semantic/type-aware grep implementations.

I'm sorry this language feature creates frustration for you and interrupts your workflow.

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

#214
post #46

Earlier quoted context omitted.

It would have been unambiguous in a world where we all agreed that both months and days start at 0 :)

Also don't forget about year 0 (which doesn't exists and is a single point of failure for so many programs that deal with calculating time between now and a BC date)

If only anything with calendars was that clear-cut. ISO 8601 does have year zero.

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

#215

# Function Shorthand #### I like how functions in js can be `arg => result`. In F# I have to do `fun arg -> result` with the `fun` keyword. It makes sense since `MyArgType -> MyResType` is a type signature in f#, but I feel like the compiler can just check if the arguments are references to types or are argument bindings. # Multiline Lists/Arrays #### I like how F# doesn't require delimiters for multiline lists. So I…

> I feel like the compiler can just check if the arguments are references to types or are argument bindings. 1. this is absolutely terrible because now you need feedback from the type checker to know how to parse the program 2. it is furthermore also ambiguous with function application , requiring arbitrary lookahead to disambiguate, also not a fun thing to do JS gets away with it because the sigil was not previously…

It doesn't need feedback from the type-checker. The type is not required, only whether a symbol is a type symbol. So it is enough to have feedback from the lexical scope, which can be tracked during parsing without any type analysis. It's a compromise, but a useful and simple one. C has been doing it since the 70s ("typedef").

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

#216

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

if it works on literals only, so a[x] doesnt work if x is negative, then ok. otherwise seems like an errors that are hard to spot.

This is such a weird take to me. You're saying: I want to add a rule, where this structure responds to a request in a certain way, based on how the programmer wrote the request in the calling code. Layer upon layer upon layer of weird, janky, edge-case, pseudo-rules, with no consistency, no clear mental model; an absolute nightmare of a programming language. No longer can you possibly intuit what a[-1] really means, nor can you intuit the rules of indexing. You've broken TWO mental models in one fell swoop. No longer can I look at your programming language and assume that anywhere I see a 7, I can replace it with a variable whose value is 7. That is no longer true in your language! Variables no longer work intuitively in your language. Think about that! What an absolute nightmare!

This is exactly the difference between a language like PHP and a pure functional language. PHP says: usually we want to do X, but sometimes Y, so we'll make Z which does X unless Q is true in which case T1 will be set and Y will happen most of the time when you want it assuming you called it the write way and put an @ in the right spot otherwise P will happen because I hadn't had lunch when I wrote that and it seemed like P was pretty likely to be the case when T1 was set but an @ was not written but lately I've been feeling like maybe T2 should also be set sometimes so if you call Z and you want X but T1 is written and you don't want to write an @ then you can just set CONSTANT_FOO_BAR_WITHOUT_X_SET_AT to 17 because the other 16 codes are already used for other things.

Functional languages say: what if everything was just math?

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

#217
post #28

Clojure’s loop expression hits this spot for me. It sets a recursion point to which you can jump using any logic inside the body you want, as long as it is from tail position. It’s like a while loop turned into an expression. I haven’t encountered any other way to write iterative expressions whose number of iterations isn’t known at the top (like map and reduce).

Tail call optimization can get you that, too. If you've written Scheme and/or gone through SICP you might be familiar with this: you write a recursive function, with the recursive function call as the last thing the function does ('tail-recursion'), and the compiler/runtime is able to optimize those recursive calls out rather than consuming one stack frame of space per call ('tail call optimization'). Clojure has loo…

Oh I didn’t know it was kind of a workaround. I do like the fact that loop is not a function though but an expression like if or case.

FWIW I think in Clojure you can use “recur” inside functions too to specifically indicate tail call recursion without relying on automatic optimization

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

#218
> Quality-of-life features that aren’t too hard to add, and don’t meaningfully change a language in its absence. Often syntactic sugar

This is basically the premise of Project Coin, released in Java SE 7: https://openjdk.org/projects/coin/

    The goal of Project Coin is to determine what set of small language changes should be added to JDK 7. That list is:
    
    * Strings in switch
    * Binary integral literals and underscores in numeric literals
    * Multi-catch and more precise rethrow
    * Improved type inference for generic instance creation (diamond)
    * try-with-resources statement
    * Simplified varargs method invocation

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

#220
post #209

Earlier quoted context omitted.

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 arr…

The point made is independent of the integer size and I think we should assume in good faith, that uint8 was chosen for the purposes of an example.
Post reply on HN