Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

261–270 of 539 posts

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

#261

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

It does! Till you hit the code of someone that didn't really care about readability that much and you're in for ugly mess.

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

#262

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

Yes please, there was nothing on the article's list that particularly resonated with me but I really wish this was standard in every language.

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

#264

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.

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

#265

I'm partial to for-else-loops. They fit perfectly into languages with compound expression (produce a value from a break in the loop or from the else block). They don't come up that often, but when they do they're really the best solution.

They also seem to be unknown to a lot of Python programmers unfortunately; I’ve had PRs rejected because a for-else loop was “unusual syntax” and therefore considered hard to maintain.

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

#266

Elixir's sigils are amazing. There are date sigils that allow you to do what the OP does: ~N[2023-01-01 12:00:00] But you can also define your own sigils to create new "custom syntax" for almost any struct. Kind of a special case of reader macros, I guess. Very convenient.

Swift has the expressibleby Type literal series of protocols for this. For example you could write an extension on Date to add initialization from a string: extension Date: ExpressibleByStringLiteral { public init(stringLiteral value: String) { // parse the string here. } } You can then do things like: let happyNewYear: Date = “2023-01-01 12:00:00” There are a protocols for all literal types. For example, you could i…

Well even JavaScript has custom string literal templates. Is this really that special?

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

#267
post #183

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

qw was great. Even in Python I sometimes write: 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',…

Ruby was inspired by Perl and also has this. Pretty great

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

#268
post #228

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

While I agree, I've met people who think the idea of a negative index is completely absurd. Their brains seem to immediately reject the concept.

There are always some of those, for every novelty. Sometimes it's me.

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

#269
post #163

Earlier quoted context omitted.

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

The problem is when you have the functions: Square.computeArea() Circle.computeArea() Clearly these should do different things. I suppose "computeArea(shape)" does dynamic dispatch based on the type of shape? But you're still putting every function defined on every type in your entire codebase in a global namespace. It's not obviously awful but I'd definitely be a bit nervous about it.

You only need dynamic dispatch if dynamic types are involved. If you can always resolve to concrete types, then you can statically resolve the required function.

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

#270
Ruby's Enumerable module is incredible and jam packed with features like .tally, .any?, .take, .partition, etc.

But one of the things I love most is how seriously they take having their Hash be enumerable. I love being able to loop through any hash as easily as you would with an array

Post reply on HN