Microfeatures I'd like to see in more languages
181–190 of 539 posts
Re: Microfeatures I'd like to see in more languages
#182With regard to strings, they give a good example in Lua, but oh boy wait until this person hears about Perl (: There's a whole section in the manual [1] for string quoting operators (qq, qw, qx, ...) In general, I feel like Perl is one of those languages that has a high amount of these "quality of life" syntactic features, and helps make it enjoyable to write, once you get over the learning curve. [1] https://perldoc…
That's actually a great point. Perl has so many syntactic sugars that it is a poster child for too much variety in ways you can write things. Making it much harder to read someone else's code.
Re: Microfeatures I'd like to see in more languages
#183With regard to strings, they give a good example in Lua, but oh boy wait until this person hears about Perl (: There's a whole section in the manual [1] for string quoting operators (qq, qw, qx, ...) In general, I feel like Perl is one of those languages that has a high amount of these "quality of life" syntactic features, and helps make it enjoyable to write, once you get over the learning curve. [1] https://perldoc…
usernames = '''
foo bar baz
hello world
'''.split()
# instead of this, which needs too many keystrokes
usernames = ["foo", "bar", "baz", "hello", "world"]
Interestingly, Python named tuples have similar interface for fields: # all of these are equivalent
EmployeeRecord = namedtuple('EmployeeRecord', ['name', 'age', 'title'])
EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title')
EmployeeRecord = namedtuple('EmployeeRecord', 'name age title')Re: Microfeatures I'd like to see in more languages
#184Negative array subscripts. So a[-1] means the last element of an array, a[-2] means the second last, and so on.
Any language that supports overriding the index operation should support this. You should be able to do this in C# with a struct with a backing array, for instance. If you're going to do this, use the word "Circular" in it, and I would also insist that if a has 4 elements, then a[0] == a[4] == a[8]. In other words, you always just take the (positive) index modulo the size of the area. Then a[-1] is the same as a[N-1]…
For instance if you have an int array that contains the numbers 1-250 and you index with a uint8 variable i,
for (uint8 i = 247; i++;) {
// print circ_arr[i]
}
for the values of i near the overflow points of the circular array and of the uint8 it gets weird: i circ_arr[i]
247 247
248 248
249 249
250 250
251 1 # 251 % 250 = 0
252 2 # 252 % 250 = 1
253 3 # ...
254 4
255 5
0 1 # i overflows to 0
1 2
...Re: Microfeatures I'd like to see in more languages
#185Omitting parens in a function signature when calling with one or no arguments.
Re: Microfeatures I'd like to see in more languages
#186 class Foo {
@Max(10) int bar;
@NonNull String name();
}
var field = @Foo::bar;
var max = @Foo::bar.Max;
var method = @Foo::name;
var foo = new Foo();
var name = method(foo);
var bar = foo.field;Re: Microfeatures I'd like to see in more languages
#187> Instead of writing 10000500, you can write 10_000_500, or 1_00_00_500 if you’re Indian. I hate this so much. It means I can't grep for a constant.
// we don’t want more than 100m because …
quota = 1000_000_000
Where people assume the underscores are in the expected place.Re: Microfeatures I'd like to see in more languages
#188Clojure’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).
Tail call optimization can get you that, too. If you've written Scheme and/or gone through SICP you might be familiar with this: you write a recursive function, with the recursive function call as the last thing the function does ('tail-recursion'), and the compiler/runtime is able to optimize those recursive calls out rather than consuming one stack frame of space per call ('tail call optimization'). Clojure has loo…
Re: Microfeatures I'd like to see in more languages
#189One of the best truly micro features I've seen recently (can't remember which langauge unfortunately - it wasn't a mainstream one), is general binary literal syntax of the form: 0x[de ad be ef 00] So much nicer than the usual condensed format. And I think it'd be valid syntax in any language that allows binary integer literal.
[1] https://elixir-lang.org/getting-started/binaries-strings-and...
Re: Microfeatures I'd like to see in more languages
#190> Instead of writing 10000500, you can write 10_000_500, or 1_00_00_500 if you’re Indian. I hate this so much. It means I can't grep for a constant.
If you have constants repeated throughout the codebase, you can pull them into a constants file, and then you can go to that file, navigate to the definition of interest, and use your IDE of choice to find usages. You'll also be able to give these constants semantically significant names, and comment next to them providing derivations or citations. And of course, if it's a mistaken or outdated value, you can change i…