Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

221–230 of 539 posts

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

#221

Elixir's sigils are amazing. There are date sigils that allow you to do what the OP does: ~N[2023-01-01 12:00:00] But you can also define your own sigils to create new "custom syntax" for almost any struct. Kind of a special case of reader macros, I guess. Very convenient.

Swift has the expressiblebyTypeliteral series of protocols for this.

For example you could write an extension on Date to add initialization from a string:

    extension Date: ExpressibleByStringLiteral {
        public init(stringLiteral value: String) {
             // parse the string here. 
        }

    }

You can then do things like:

    let happyNewYear: Date = “2023-01-01 12:00:00”

There are a protocols for all literal types. For example, you could implement ExpressibleByIntegerLiteral and have have it init the Date object from a unix timestamp. There is even an ExpressibleByNilLiteral.

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

#222
post #76

Python's `with` Function composition operators eg a |> b # Call `b` with `a` as an argument b Then you can do something like let x : Map = collect map(entries) |> flatten

I don't get it, how are |> and <| any different from parentheses?

Call/Pipe operators are just sugar for normal function calls. It's nice because adding or removing a call doesn't require balancing parentheses. It's helpful for writing stream or sequence/iterator based code and throwing debug utilities in the middle.

It's less of an issue if your language has UFCS or other postfix function call syntax like mentioned in this thread, but if you don't this is nice to have.

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

#223
post #64

> Second, what if parameter blocks were abstractable? Sounds like a great way to make unreadable code #define STANDARD_EXP_PARAMS ... // I hit gotodef on func(...) and got here. What are the arguments? void func (STANDARD_EXP_PARAMS) { // ... long function body ... x = y; // What is the type of x? Is it an argument or a local? } I'd really be irritated if someone ever used this feature, optimizing for lines written i…

You could say the same about a function call or a function that takes an object as an argument - who knows what code lies behind the impenetrable barrier of structured or object oriented programming?

That's actually a feature of encapsulation. The object (ideally) is used to abstract away those details so I don't need to know about them, only how to create the object. Encapsulation is a different feature than reuse.

The exception is in languages that use that syntax as a way to implement keyword arguments, but I would still ask people to destructure it in the parameter list or at the top of the function so I can see what the arguments are.

The point is that when I am looking at a function definition I need to know how to call it. As described all I see is obfuscation to save some keystrokes, not clean code.

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

#224
post #28

Earlier quoted context omitted.

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…

Interestingly, I almost prefer Clojure's `recur` semantically. Means you don't have to change the function name twice if you rename it, and it's hard to miss that you're recursing.

If you don't have to change a function's name twice when you rename it, that implies it is not called anywhere. :)

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

#225
post #126
post #118

Earlier quoted context omitted.

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

Okay, that would confuse me. Looks very similar and easy to overlook.

Syntax highlighting can help with distinctions like that.

You might prefer Nim's visually distinct "let" for immutable, "var" for mutable. "const" is also available and means resolved to constant at compile time, similar to Zig's "comptime".

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

#226

Agree with kebab-case. > Most languages have multiline literals, but what makes the Lua version great is that the beginning and ending marks are different characters. This solves the infuriating “unnestable quotes” problem string literals have, and you don’t have to escape all your literal \s. That paragraph also uses “nestable marks”.

Nestable comment syntax is also nice. At least some MLs (eg SML) has it, that I know of.

Indentation-sensitivity can also solve similar problems. (Indentation does not have to exclude requiring graphic termination. A formal language can require both. Or just a helpful tool.)

(Also agree with 'kebab-case', although the name is new to me and a bit weird.)

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

#227
post #181

Omitting parens in a function signature when calling with one or no arguments.

This means you can’t reference the function itself by name without some kind of quoting construct or other circumlocution; also, while the no-arg case might make sense as a special case, if you allow the one arg case, you might as well admit the general case.

The single argument case works fine for Lua but maybe the no argument case woudn't. Having to supply only the empty argument list seems counterintuitive, though.

Variable sigils would work but that would be even more annoying.

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

#228

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

While I agree, I've met people who think the idea of a negative index is completely absurd. Their brains seem to immediately reject the concept.

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

#229

Earlier quoted context omitted.

That's for languages that can't define arrays with custom start/stop indexes. But those that have custom indexes they can very easily expand/implement as helper class (for example array.indexFromLast(1) which means array[Length(array)]. This way you can have best of both worlds.

Surely if your language has custom indexes / ranges `Length(array)` is completely broken and the language provides something like "Index`Last" you can hook on? Because an array with indexes [3, 7) has length 4, but 4 is not the index of the last element.

As long as there exists a bijection between whatever you choose as an index and the natural numbers starting from 0 it is fine. (I.e. the range of valid indices must be a countable set) In your example that bijection could be:

  3 -> 0
  4 -> 1
  5 -> 2
  6 -> 3
This works for vectors as well, so why not have a range from (0,0) to (5,5) to index into an array arr? You could write the function that does the mapping manually:

  arr[(x,y)] = backing_array[x / 5 + y] //bounds checks omitted
But here it can be automated quite simply to allow for vectors of even 3 or 4 dimensions.

Just know that custom indexes / ranges are not automagically broken. Personally, I like how much easier it is to read the intent with custom indices.

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

#230
post #219

Earlier quoted context omitted.

But you need to search for both hex and dec codes for that

Yup which is still a one liner in grep

You can grep the snake case too in one grep

You can do almost anything in a grep

Post reply on HN