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…
Microfeatures I'd like to see in more languages
171–180 of 539 posts
Re: Microfeatures I'd like to see in more languages
#172Elixir’s testing library uses meta programming to show the code that fails and what the values were at both sides of a comparison. IE a = 1; b = 2 assert a == b Will fail with error like: Assertion failed, a == b Left is 1 Right is 2 So you don’t have a bunch of assert-functions; you just assert anything and it will spit out a decent error.
e.g. in pytest it won't just print out the values of "a" and "b", it will recursively document intermediate values until it's reached the toplevel expression:
assert f() == g()
assert 42 == 43
where 42 = .f at 0xdeadbeef0002>()
and 43 = .g at 0xdeadbeef0003>()
and it's possible to customise the report so you can report as a diff: assert "foo 1 bar" == "foo 2 bar"
- foo 2 bar
? ^
+ foo 1 bar
? ^Re: Microfeatures I'd like to see in more languages
#173Example: https://github.com/mchrisman/variables-with-units-language-p...>
Re: Microfeatures I'd like to see in more languages
#174Earlier quoted context omitted.
> The first time it's seen in any environment, it thread-safely inserts-if-not-present the default value into a key-value storage That seems like a great way to get amazingly hard to replicate bugs or odd behaviours if different subsystems use different values for the default.
You'd just need to have a mutex lock on the values. That could be slow, but you probably don't want to have a config flag in a hot loop anyways. :) In Nim you can do compile flags which let you set constants so you avoid the problem: const myLibraryVersion {.intdefine.} = 3
Oh no they're saying that it's thread-safe, that's not an issue. Rather that depending on the order of initialisation, possibly of different systems entirely, you can have different initial states because different systems or subsystem decided of the default value.
Re: Microfeatures I'd like to see in more languages
#175Earlier quoted context omitted.
Totally true. I don't know if IDEs commonly have a feature like, "find this value, regardless of how it's expressed" (even better yet, fuzzily, to catch typos), but I think that's the proper general solution. It's a good idea to consider a constants file earlyish, while everything still fits in your head. I'd say in the case of such a sprawling system, make a constants module, and it can have different files for diff…
When your code-base reaches a certain size, you cease using the IDE to find code and instead start using specialized tools, such as Lucene indexes so that grepping through code takes seconds rather than minutes. One of the bads of this is that using regex over an index is O(n) in comparison to the O(log(n)) of a normal index lookup.
Re: Microfeatures I'd like to see in more languages
#176Elixir'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.
Eg: ~w(foo bar bat) is a word list. `~ letter bracketed-text lettersasmodifiers` desugars as sigil_(text,modifiers). Similar to foo_str() of Julia[3], but for one-letter-names and more brackets. But not the unicode brackets of Raku.
[1] https://elixir-lang.org/getting-started/sigils.html [2] https://hexdocs.pm/elixir/main/syntax-reference.html#sigils [3] https://docs.julialang.org/en/v1/manual/metaprogramming/#met...
Re: Microfeatures I'd like to see in more languages
#177# Function Shorthand #### I like how functions in js can be `arg => result`. In F# I have to do `fun arg -> result` with the `fun` keyword. It makes sense since `MyArgType -> MyResType` is a type signature in f#, but I feel like the compiler can just check if the arguments are references to types or are argument bindings. # Multiline Lists/Arrays #### I like how F# doesn't require delimiters for multiline lists. So I…
waiting = sum workers #(%.in_queue + %.in_flight)
Clojure has some syntax like this though it isn’t needed for the most obvious use-case of functions to extract fields because keywords, which are usually used for map keys, are implicitly functions that look themselves up in their arg, e.g. (:bar { :foo 3, :bar 2 }) ; => 2Re: Microfeatures I'd like to see in more languages
#178Negative array subscripts. So a[-1] means the last element of an array, a[-2] means the second last, and so on.
Re: Microfeatures I'd like to see in more languages
#179# Function Shorthand #### I like how functions in js can be `arg => result`. In F# I have to do `fun arg -> result` with the `fun` keyword. It makes sense since `MyArgType -> MyResType` is a type signature in f#, but I feel like the compiler can just check if the arguments are references to types or are argument bindings. # Multiline Lists/Arrays #### I like how F# doesn't require delimiters for multiline lists. So I…
1. this is absolutely terrible because now you need feedback from the type checker to know how to parse the program
2. it is furthermore also ambiguous with function application, requiring arbitrary lookahead to disambiguate, also not a fun thing to do
JS gets away with it because the sigil was not previously used and it only requires a single lookahead to parse, as only single-parameter anonymous functions can have "bare" parameter lists.
Re: Microfeatures I'd like to see in more languages
#180Negative 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]…