Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

471–480 of 539 posts

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

#471

Earlier quoted context omitted.

All the indexed sequential collections in the standard library use it, for starters, and those collections are in turn used by the majority of users. OTOH a convenience feature as a lock-in is hard to believe.

Inexing forwards sure. How many backwards then? And it mihjt be possible to add a static method to array to index backwards yourself (Can't remember what they are called, but look and act like methods on the object but aren't).

All standard .NET collections with defined order and O(1) indexing support indexing backwards.

And no, it's not possible to do this using an extension method, unfortunately - there are no extension properties or indexers in C# (yet; it's something that keeps coming up). But then again, if and when they add extension indexers, this arrangement with a custom type is what'd allow you to write one that does backwards indexing on a collection type that doesn't support it out of the box.

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

#472

Earlier quoted context omitted.

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.

Well, I write plenty of Python code, so it actually comes up quite often. The annoyance with Python is that it just treats negative values as magic, so if you accidentally end up with a computed negative index, it silently does the wrong thing. But the alternative approach with explicit index-from-end syntax - whether like in C# and Nim, or like Julia and Matlab - doesn't have that problem; it's pure convenience.

And yes, of course, you can always do the same in some other, more verbose way. But why should we tolerate that verbosity when there's a solution that makes code both shorter and more readable? I rather hope that more languages will adopt one of these techniques.

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

#473

I have long wondered why Ruby's symbols aren't in every language.

They exist in K/Q. A single-word identifier-shaped symbol begins with a backtick, or a multi-word symbol can be created with a backtick and double quotes. A sequence of symbols is a vector literal, and is stored compactly. For example: `apple `"cherry pie" `one`two`three Many languages will intern string literals implicitly, or allow a programmer to explicitly intern a string; for example Java's "String.intern()". Th…

https://code.kx.com/q/basics/enumerations/

i miss k

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

#474

Strongly-typed units and unit literals (e.g. 3mL, 10gal, 15m / 3s = 5m/s) AFAIK F# has these and that's about it

You can make that in Kotlin, e.g.: `3.mph` or `(-45..45).deg` for ranges.

with definition like:

    val Double.mph: Speed
      get() = Speed.mph(this)

    data class Speed(val ms: Double)
      companion object {
        fun mph(mph: Double) = Speed(mph * 1609.344 / 3600.0)
      }

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

#475

I love all the sugar of Kotlin, but there is one thing I miss, and it is the indexing of arrays of Python when pulling out part of an array.

Kotlin is so close on so many things, but then keeps messing up. arrayOf(1, 2, 3) - why not [1,2,3]? emptyArray() - why not []? mapOf("key" to "value") - why not {key => value}? These are all solved problems. Why would they make up some harshly suboptimal syntax?

`{key => value}` it should be mutableMapOf() just as well. Same for array.

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

#476
post #181

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

Kotlin has it, but only if the function accepts the function argument, e.g. `foo({println("hello")})` is the same as `foo {println("hello")}` (larger example in my other comment above).

You can also make it on no-args function, if it's a method (attached to a type). Utilizing @get(), like that: `3.mph` and have somewhere else defined

    val Double.mph: Speed
      get() = Speed.mph(this)

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

#477
post #437

Earlier quoted context omitted.

But this isn't a discussion about pickups specifically. Attacking an example is pointless if the thing you take issue with is incidental and not fundamental to the argument. I bet you could think of a case where you pick something else and OP's example meets your standards.

My point was that there is a meaningful, and I believe relatively simple, distinction to be made between free functions and methods bound to an object - a distinction which UFCS doesn't really help with. For any given example, I believe there is a reason to prefer one over the other, and I showed what reasoning I would use for the particular example raised by OP.

I got that and it's a good point.

Although I rarely see Objects used this way. Often, methods are used to implement all related functionality. Unity even strongly encourages this. (...at the moment. They are working on Entity Component Systems which will work more similar to my third example)

I concede that languages shouldn't use the dot as a syntactic tool, be it through Extensions[1] or UFCS, but rather offer a pipe-operator. If they don't, I'd still prefer UFCS rather than no way of chaining at all.

[1] Extensions for interface/protocol conformance are fine of course.

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

#478

Earlier quoted context omitted.

They can be implemented as a library C++ thanks to templates and user-defined literals, and there's a proposal to add them to stdlib based on one of the existing implementations: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p19...

You can implement it in C# too with zero run time cost.

I don't see how, except for manually rolling a struct for every new combination of units. The C# generic system is not flexible enough to define things like "m / s / s = (m/s^2)".

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

#479

Earlier quoted context omitted.

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

Well, I write plenty of Python code, so it actually comes up quite often. The annoyance with Python is that it just treats negative values as magic, so if you accidentally end up with a computed negative index, it silently does the wrong thing. But the alternative approach with explicit index-from-end syntax - whether like in C# and Nim, or like Julia and Matlab - doesn't have that problem; it's pure convenience. And…

(per your other posts, extension methods is their name. And they aren't supported here, got it).

> it silently does the wrong thing

yeah, my original complaint was this

> why should we tolerate that verbosity when there's a solution that makes code both shorter and more readable?

Because it's a balance. How much it benefits how many users to what degree vs. extra cost of implementation and maintenance. If you're not careful you go down the kitchen sink road and end up with bloat. Be careful when adding stuff cos you have to support it forever.

Anyway, thoughtful answers thanks.

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

#480
post #133

Earlier quoted context omitted.

How is that third one readable at all? I’d assume it meant integer division.

You're writing something and you decide you want to apply 'f' to it, so you type '// f' (instead of backspacing like a caveman). It's actually rather convenient.

Ah neat.
Post reply on HN