Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

321–330 of 539 posts

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

#321
post #317

Earlier quoted context omitted.

You can do something similar in JavaScript > const love = "love"; > { // Section > console.log(`What is ${love}`); > }

Not the same ergonomic. The language doesn't "understand" it's a section (no opportunities listed in the original comment).

Actually, it does, though the only real use is with the `break` keyword for breaking out of a labelled block (or break / continue in a for loop): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Things like stack traces don't track them, unfortunately.

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

#322
post #42
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…

and vice-versa Doesn't the reverse pollute the function namespace? If every obj.fun() can be written as fun(obj), doesn't that cause ambiguity with a previously imported global function fun()?

Not unless the function signatures overlap entirely. That's rare enough that I don't think I've ever? encountered it in several years of writing Nim.

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

#323

If unit testing makes the cut, then I think standardized documentation and doctests should be included also. Python (string literal at the beginning of a class/function), C# (structured comments) and Rust (doc attributes) are three different, valid ways of adding this to the language.

D has had these for a long time.

Looks like these fall into the "structured comments" category, based on seeing /** */ and /// in a repository I found. Does it also have a story for doc tests?

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

#324

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

I'm fine with an array type that supports this, but not as the default. I've been bitten in the past by code that ran error-free while giving incorrect output due to this feature suddenly making the indexing valid. I'd prefer it to be opt-in somehow so that the default behavior for negatives is invalid, not silently wrong yet valid behavior.

Matlab has the opposite where your syntactically valid but mysteriously non-working code suddenly has a large array.

EDIT: On the other hand, I think Matlab's array(end - number) indexing syntax is a good compromise of convenience and less error prone explicitness.

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

#325
post #312

I really adore Python's iterable- and keyword-unpacking operators (*some_iterable, **some_mapping): arr = [0, 1, 2, 3, 4] head, *body, tail = arr # head=0, body=[1, 2, 3], tail=4 head, *rest = arr head, *_, tail = arr *_, tail = arr first, second, third, *rest = arr foo = {"a": 1, "b": 2, "c": 3} bar = {"b": 9, "x": -1} {**foo, **bar} # -> {"a": 1, "b": 9, "c": 3, "x": -1}

why bother with the distinction between one * and two *? in javascript the … operator neatly does both

Python has keyword arguments to functions. In that context, a single star packs or unpacks a list of positional arguments and a double star packs or unpacks a dictionary of keyword arguments.

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

#326
About kebab-case identifiers.

What I always wanted is proper spaces! Designing syntax where identifier could have spaces (without backticks or something like that) might be tricky of course. But may be it's not impossible.

All those space imitations, whether they're dashes, underscores or camels - they're just imitations. Nothing compares to real spaces.

If anything, underscores are closest ones, if you ask me.

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

#327
JSON compatibility, to be able to copy and paste from JSON to valid nested dynamic arrays.

  a = {
    "a": "a",
    "b": {"b": 2} 
  }
Optional commas at the end of lines, so this is also valid

  a = {
    “a“: "a"
    "b": {"b": 2} 
  }
and we are able to swap or append lines without editing the commas or forgetting to do it and get a syntax error. Mandatory commas on all lines would do but it gets in the way of JSON compatibility.

This must also be legal code and equivalent to the previous one

  a1 = {
    a: "a",
    b: {"b": 2} 
  }
  a == a1 # true
The developer decides when saving typing time is more important than JSON compatibility.

PS: a big yes to kebab-case too. That's in part CSS compatibility because CSS class names are often kebab cased.

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

#328
post #277

this is usually a style thing not an enforced syntax and maybe a hot take but I actually really like leading commas in comma-delineated lists (like Elm and Haskell). Makes changing the order of things really convenient

Agreed. Especially the more diff-driven development we do.

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

#329
post #286

I might add kebab-case to my current language project. From all the code I've written, I only found a handful of - operators not surrounded by spaces, so that ambiguity wouldn't bite me often. Also, I wish the unary negation operator was more visually salient. `foo * -bar` is very different from `foo * bar`, but it's only a handful of pixels on the screen. I've thought about trying to render it as an em-dash or somet…

There are coding fonts that make the "-" look more like a minus than a hyphen. I know that Iosevka and its variants do.

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

#330
post #138

Earlier quoted context omitted.

Also on a function? var getDate() { return „no date“; }

That is often considered undesirable because it makes code less clear and compilation errors inscrutable. For instance the rust developers consciously decided to remove that from the language, named functions must be fully typed.

Easter egg: you can use -> _ to ask the compiler what type it thinks the return type should be, given your body. Because it is only used for diagnostics it isn't fully featured and there are things it doesn't cope well with, but it is there and works most of the time.
Post reply on HN