Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

481–490 of 539 posts

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

#481
post #216

Earlier quoted context omitted.

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

I disagree about purity. At some point "math purity" "math correctness" may be not desirable.

In this case everything is about intention

In general accessing index out of range (above or below) is not desirable, in almost all cases this is bug.

And now, in my opinion `array[-1]` when `-1` is hardcoded would tell, with full intention that last index is desired.

Basically it would be translated to `arr[arr.Length - 1]`. You don't write code with `array[-1]` because that's clearly wrong (when there's no going back behaviour)

Meanwhile when it is calculated, then it should result in an error.

The rules are pretty simple I'd say - if you desire to use "reverse syntax" then you can, but when you use variables with may be calculated wrongly, then you will receive an error.

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

#482

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

Only downside I can see is performance hit. You need to check if it’s negative then you need now calculate the length first… which is a bit of overhead but considering how often you use arrays in a tight-loop… I mean if you can optimize out the check because you can detect that the index is always positive…

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

#483

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?

Coming to Kotlin from Python, these always get on my nerves. My brain hasn't adjusted to needing to call a function. And 'X to Y' just doesn't click for me. I guess I'll get used to it eventually

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

#484

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

Only downside I can see is performance hit. You need to check if it’s negative then you need now calculate the length first… which is a bit of overhead but considering how often you use arrays in a tight-loop… I mean if you can optimize out the check because you can detect that the index is always positive…

You can have an optional length prepended to arrays that use that feature.

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

#485

Earlier quoted context omitted.

grep '[0-9_]+'|sed 's/_//g'|grep

That's kind of a handful to type every time :) I'd just do for the first one and then scan for the number I'm interested in. If you have more than a page full of constants..

You can make it an alias. No need to type it every time!

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

#486
post #371

Earlier quoted context omitted.

We don't have to follow locale, we could just force ISO 8601 and there won't be any ambiguity.

Yeah, but this is Windows we're talking about. Even the SCHTASKS program (kind of like /usr/bin/at) takes a date which is locale sensitive, making it absolutely useless for scripting. Check out this answer to a question about how to use it: https://stackoverflow.com/a/18730884

The language doesn't have to care about $PLATFORM, if the spec says dates in source are ISO 8601 they're ISO 8601 or they're invalid.

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

#487
post #9

Earlier quoted context omitted.

I think you can also do the second in Go with named returns, e.g. func sum(nums []int) (result int) { for _, n := range nums { result += n } } No clue if it's an idiomatic usage, and named returns always felt a little too magic for me.

Lots of languages will implicitly return the final expression— I feel like that's a decent compromise. Not quite as magical as an actual named variable that just exists, but not as clunky as needing as explicit `return` every time.

I always disliked implicit returns, and over the years and having dealt with many more codebases, some quite large, I've learned to dislike any implicitness.

I would much prefer that you must explicitly return a value (even if it's through an implicitly declared 'Result' variable) rather than just 'try to guess what happened here, in this long function with lots of expressions'.

There are a few exceptions, like Forth, where you really have to keep the current state of the stack in mind at all times anyway. Those exceptions naturally tend toward very small functions. Most languages don't, and the result is inevitably difficult to understand bugs.

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

#488

> Frink has special syntax for date values. You can write # 2001-08-12 # to mean the date 2001-08-12 Frink? Frink ? This Visual Basic erasure can not stand. The #-delimited date literal syntax has been found all over the VB family: Visual Basic, VBA, VBScript, and it persists to this day in Visual Basic .NET. Of course, being a VB syntax, it's completely cursed. You can put # 01/05/2023 # in a VB file, and what date…

Or you could just use the standard '2001-08-12' with no special syntax needed and avoid the problem altogether. ISO-8601 or it's invalid. There's no reason to allow other formats or need other syntaxes when we have a 35 year old standard to use.

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

#489

> Frink has special syntax for date values. You can write # 2001-08-12 # to mean the date 2001-08-12 Frink? Frink ? This Visual Basic erasure can not stand. The #-delimited date literal syntax has been found all over the VB family: Visual Basic, VBA, VBScript, and it persists to this day in Visual Basic .NET. Of course, being a VB syntax, it's completely cursed. You can put # 01/05/2023 # in a VB file, and what date…

There’s a Java compiler plugin, manifold, that makes this type of syntax easy. There’s an example covering the date “literal” case…

https://github.com/manifold-systems/manifold/tree/master/man...

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

#490
post #419

Earlier quoted context omitted.

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.

Ecstasy uses % for modulo, and /% for divrem (division and remainder). So "a = b % c" for calculating the modulo, but "(a, r) = b /% c" to get the divisor and remainder.
Post reply on HN