Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

101–110 of 539 posts

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

#101
post #9
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 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.

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

#102

Php supports kebab-case variables: ${"variable-name"}=123; Isnt it beautiful?

Perl, too. (Probably not a coincidence.)

You can also put a newline in a variable name if you really want. Or a 0 byte.

Here's a demo. I've used the debugger because its "X" command can print the true name of the variable:

    $ perl -d -e 1

    Loading DB routines from perl5db.pl version 1.60
    Editor support available.

    Enter h or 'h h' for help, or 'man perldebug' for more help.

    main::(-e:1):       1
      DB ${"variable-name"} = 123;

      DB ${"variable\nname"} = 456;

      DB ${"variable\0name"} = 789;

      DB X ~variable
    $variable^@name = 789
    $variable^Jname = 456
    $variable-name = 123

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

#103
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()?

In rust you can do this but they are namespaced with the name of the struct. So you can do Object::fun(obj)

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

#104
post #46

> 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

It would have been unambiguous in a world where we all agreed that both months and days start at 0 :)

Also don't forget about year 0 (which doesn't exists and is a single point of failure for so many programs that deal with calculating time between now and a BC date)

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

#105
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()?

If you have two functions abracadabra(foo) and foo.abracadabra() which do different things, you should rename one of those functions tbh.

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

#106
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()?

> doesn't that cause ambiguity with a previously imported global function fun()?

Yes, although overloading can mitigate the issue.

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

#107

Clojure’s loop expression hits this spot for me. It sets a recursion point to which you can jump using any logic inside the body you want, as long as it is from tail position. It’s like a while loop turned into an expression. I haven’t encountered any other way to write iterative expressions whose number of iterations isn’t known at the top (like map and reduce).

Is that like using "continue" in most C-syntax languages?

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

#109
post #99
post #42

Earlier quoted context omitted.

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()?

In Rust, it's still qualified by the type you're calling it on, which I assume is also the case in other languages.

> which I assume is also the case in other languages.

Nope. Rust has an extremely restrictive form of UFCS. In fact officially it's not called UFCS, but "Fully Qualified Path syntax".

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

#110
local const variables like JavaScript has them. You can declare variables with „let“ (mutable) or „const“ (immutable). This is really great when reading code, because you never have to check if some code may change the variable at some point. And you usually declare most variables as const.

A lot of languages provide immutable variables only for class members or statics, but not for local variables.

Post reply on HN