Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

441–450 of 539 posts

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

#441
post #4

My favorite is uniform function call syntax. In several languages (Nim, Koka, D, …), you can always write bar.foo(baz) instead of foo(bar, baz) and vice-versa. Another one from Nim is the implicit result variable. Instead of having to do this: func sum(nums: seq[int]): int = var result = 0 for num in nums: result += num return result you just do this: func sum(nums: seq[int]): int = for num in nums: result += num It…

I don't like they implicit result variable from a scope point of view.

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

#442

For rust, it is probably the try (?) operator. Fundamentally, it's just syntax sugar for a match statement with an early return in the Error or None cases, but it really improves the ergonomics of dealing with Result and Option types.

Interestingly, there was once a solid effort to add a try operator to Go. While the proposal was quite well received, upon closer inspection it was realized it would be essentially useless in the real world as, given how the rest of the language works, you almost never would want to simply early return with the value received. The data revealed that the vast majority of the code in the wild that the syntax sugar woul…

Is there a link to this discussion? This seems interesting. If I can’t make a call to a downstream service, or a file I’m trying to read doesn’t exist, or a s3 bucket 404’s, or almost any other “real world” error I can think of, the only (sane) way I can think of handling this is propagating the error down to the caller (and perhaps logging?) Do you mean to say that it is idiomatic in Go to handle errors by… doing something else?

On mobile so I can’t put in a code block, but here’s how I thought Go was written: value, err := some_fn()

If err != nil { Return err }

(? Operator works here because you could do value := some_fn()? And remove the if statement boilerplate)

Do you mean that instead of “return err” Go idiomatically does something else?

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

#443
post #4

My favorite is uniform function call syntax. In several languages (Nim, Koka, D, …), you can always write bar.foo(baz) instead of foo(bar, baz) and vice-versa. Another one from Nim is the implicit result variable. Instead of having to do this: func sum(nums: seq[int]): int = var result = 0 for num in nums: result += num return result you just do this: func sum(nums: seq[int]): int = for num in nums: result += num It…

I've only seen this proposed as a feature to help with template meta-programming (though it could also make similar sense in any dynamic language). There, it helps if a template can say `t.foo()`, and, with UFCS, can use that template with any t for which either `foo(t)` or `t.foo()` exist. In contrast, in C++ today, a template using `t.foo()` limits its own use only to types that have a foo() method, probably unnece…

What do you mean by “twice as many places”? And looking up definitions is a job for the language server.

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

#444

For rust, it is probably the try (?) operator. Fundamentally, it's just syntax sugar for a match statement with an early return in the Error or None cases, but it really improves the ergonomics of dealing with Result and Option types.

Interestingly, there was once a solid effort to add a try operator to Go. While the proposal was quite well received, upon closer inspection it was realized it would be essentially useless in the real world as, given how the rest of the language works, you almost never would want to simply early return with the value received. The data revealed that the vast majority of the code in the wild that the syntax sugar woul…

[deleted]

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

#445

> You can write # 2001-08-12 # to mean the date 2001-08-12, instead of writing something annoying like Date(2001, 8, 12) I like this article but oh man dates just trigger me. Such a missed opportunity to use an unambiguous date example like 2001-08-13

Oh the joys of being a school administrator with a new international student whose birth date is given as 3/7/5.

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

#446
The "Expanded Parameters blocks" is kind of supported in Kotlin. You can write a function like this:

    /**
     * @param arg the string to garble
     */
    fun doThing(@NotEmpty arg: String = "default)
In this example, "arg" is mandatory, or else it would have the type "String?", making it nullable. It obviously has a default value. It has an annotation that performs some validation, though admittedly that is a library and not a language feature. And it has its own documentation. I find this more concise than the Powershell example.

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

#447
post #137

Perl's if and unless operators, which can also be postfixed, eg: die "can't be negative" unless $i >= 0; Perl is full of usability features, like for reading input, inline literate coding annotations, implicit $_.

Similar in spirit to the `until` loop.

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

#448

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.

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

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

#449
post #369

Earlier quoted context omitted.

What 'mod' is for, innit.

Right, cause why let the language do it automatically when you can use a bug prone manual implementation?

If you can't correctly implement a circular buffer with mod (or 'and') then the language can't save you, nothing can.

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

#450
post #4

My favorite is uniform function call syntax. In several languages (Nim, Koka, D, …), you can always write bar.foo(baz) instead of foo(bar, baz) and vice-versa. Another one from Nim is the implicit result variable. Instead of having to do this: func sum(nums: seq[int]): int = var result = 0 for num in nums: result += num return result you just do this: func sum(nums: seq[int]): int = for num in nums: result += num It…

I don't like they implicit result variable from a scope point of view.

What do you mean?
Post reply on HN