Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

141–150 of 539 posts

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

#141
post #116

One 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.

A lot of languages allow underscores in numeric literals. Something like 10_000. You can put them anywhere and they get ignored. I don’t know if they also allow it for hex numbers.

[deleted]

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

#142

Haskell has almost all of these. > Instead of writing 10000500, you can write 10_000_500, or 1_00_00_500 https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/nume... > Balanced string literals https://hackage.haskell.org/package/raw-strings-qq-1.1/docs/... > Generalized update syntax Use Lens. `fileName %~ max 2` > you can write the sequence 1, 2, … n-1 as 1.. Yup. `[1,2..n-1]` There's far more to it, you have acc…

>> Symbols

> In Haskell you use hash has a prefix instead of colon.

Can you give an example?

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

#144
post #56

Earlier quoted context omitted.

That’s not enough — the underscores can appear anywhere, so you need to cater for 500000_0 and 5_0_0_0_0 etc. basically an optional underscore between each digit.

just grep for `[0-9_]+`

I would assume they're looking for a specific constant value.

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

#145

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.

The most recent addition to the digit family being Phoenix’s new ~p”/healht”, which is a HTTP route string that automatically verifies whether the route exists, and returns compile-time warnings when you link to a path that didn’ doesn’t. It’s fantastic, and really surprising it took this long to be added to any web framework.

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

#146
post #68
post #51

Earlier quoted context omitted.

Depends on the size of your codebase and how many people work on it. Once you have 20 years of code written full time by 200 developers in your monolith, finding the constants file out of 400 different subsystem's constant files for a given subsystem that you've never seen before can become a legitimate and challenging pain.

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

#147
# 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 can do `let myList = [1; 2; 3]` or

    let myList = [
      1
      2
      3
    ]
# Regex Literal ####

I like how in Crystal instead of doing `/my[regex]/` i can do `%r(my[regex])` where the parenthesis can be any brace type (like "(", "{", "# Argument Accessor Shorthand ####

In Crystal, you can use an ampersand to bind and access a property on an object, instead of writing the verbose form with a function.

So this

    ["a", "b"].join(",") { |s| s.upcase }
can be written as

    ["a", "b"].join(",", &.upcase)
If this were available in F#, for example, instead of

    ["a"; "b"] 
    |> List.map (fun s -> s.ToUpper())
    |> String.concat ","
I could do

    ["a"; "b"] 
    |> List.map &.ToUpper()
    |> String.concat ","

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

#148
Elixir’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.

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

#149
post #131

> [In Chapel] there’s the config keyword. If you write config var n=1, the compiler will automatically add a --n flag to the binary. As someone who 1) loves having configurable program, and 2) hates wrangling CLI libraries, a quick-and-dirty way to add single-variable flags seems like an obvious win. Letting people define configurable variables at their call site is incredibly valuable, even if you don't have compile…

> 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.

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

#150
post #64

> Second, what if parameter blocks were abstractable? Sounds like a great way to make unreadable code #define STANDARD_EXP_PARAMS ... // I hit gotodef on func(...) and got here. What are the arguments? void func (STANDARD_EXP_PARAMS) { // ... long function body ... x = y; // What is the type of x? Is it an argument or a local? } I'd really be irritated if someone ever used this feature, optimizing for lines written i…

You could say the same about a function call or a function that takes an object as an argument - who knows what code lies behind the impenetrable barrier of structured or object oriented programming?
Post reply on HN