Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

411–420 of 539 posts

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

#411
post #79

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

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 always just take the (positive) index modulo the size of the area.

That's something I'd like in a bunch of languages - a real modulo operator that always returns between 0 and n, even for negative inputs, rather than a remainder operator that's advertised as a modulo operator. Grrrrr!!!!!

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

#412

Earlier quoted context omitted.

`$` refers to the length of the array, so `d[$]` is an array-bounds error. `d[$-1]` is needed for the last element, but you can’t do that blindly, you have to check that the length is nonzero or you’ll get unsigned underflow to ulong.max

perl have $d[$#d] represent the index of last element.

Yeah, but $#list is better used for writing loops:

  foreach my $i (0..$#list) {
    say "$i: $list[$i]";
  }
For getting the last element from a list you can just use -1 (and of course further negative numbers work like you would expect, -2 is second to last and so on):

  my @last_three = @items[-1, -2, -3];

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

#413

Absolutely damned is combination of Kotlin's several features: 1. Lambda functions can be defined with `{}`. 2.`foo(bar, somefunc)` is the same as `foo(bar) somefunc`. In other words, if the last parameter is a function, it can be provided AFTER closing parenthesis. 3. Interfaces that require only one method can be implemented on-side with a lambda function (i.e. `{}` syntax for no-param function). Combined those thr…

Kotlin's features are amazing for designing DSLs but I think your code sample uses more than just the 3 points you mentioned. Specifically the `call.respondText(..)` part. I assume that in this example the second argument to the `get` function is actually a "lambda with receiver", which means that the lambda executes with another object as the receiver (and that object is bound to "this" inside the lambda block), which makes the `call` object available.

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

#414

Earlier quoted context omitted.

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.)

What makes C-style comments unnestable?

/**/ could potentially be nestable, they are just not defined as such.

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

#415
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

'with' is cool, but it is annoying that it creates a new scope. And then you have ExitStack if your lifetimes do not neatly map to scopes. And AsyncExitStack and async with if you happen to be in an async function.

I prefer RAII.

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

#416

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

> In F# I have to do `fun arg -> result` with the `fun` keyword

meanwhile in C++:

  [&](auto arg) { return result; }
You don't know how good you have it.

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

#417

Earlier quoted context omitted.

C# has a very nice approach to this: indices aren't simple numbers, but values of type Index [1], which store both the offset and the direction, and can be implicitly created for plain ints. When you do want to index from the end, you use the unary ^ operator to create a reverse index. Thus, you can write things like a[^1] or a[0..^1]. But, more importantly, it means that any custom collection type can define an inde…

That's a lot of machinery which I feel is going to benefit relatively few people and cases. I suppose I should learn it just in case but my suspicion is that MS is adding extra stuff which they hope people will use which will act as a lock-in to C#. Ergo the benefit of this is to MS not to the end user ISTM.

@int_19h, @xigoi perhsps you're right but how many times have you ever indexed backwards? Other, I grant, than to get the last item in a list. If it's more general then reversing the list would be better, alternatively you might have

   lst.reverse()[x]
which the compiler could guasrantee to recognise and simply implement as a calculation.

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

#418

Earlier quoted context omitted.

You already can't, because someone can write the constant in hex, or even (shudder) octal.

Or like this: const int WAIT_TIME_MICROSECONDS = 42 * 1000 * 1000; // 42 seconds

Or:

  0x2A * 10e6;
The solution is clearly to have language tooling with a find-constant tool which you give an expression and it parses all declarations in the source code looking for one which evaluates at compile time to the provided expression.

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

#419
post #316

Earlier quoted context omitted.

C#'s % is explicitly the remainder operator, not the modulo operator.

Not according to reflection. Expression > lambda = n => n % 2; Console.WriteLine(((dynamic)lambda).Body.NodeType); Output is "Modulo".

Hmm... I haven't read the official spec, just the Microsoft documentation: https://learn.microsoft.com/en-us/dotnet/csharp/language-ref... In any case, the behavior is rem, not mod.

Ada has both rem and mod operatiors. I'm not sure how many other languages have operators for both.

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

#420

Earlier quoted context omitted.

D's approach would be: 0xde_ad_be_ef_00

Wouldn't endianness interfere with this notation and the preceding one?

Of course it does - endianness interferes with any sort of numeric literal. Doesn't matter if it's hex, decimal, underscored, whatever.
Post reply on HN