Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

161–170 of 539 posts

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

#161

Earlier quoted context omitted.

That's for languages that can't define arrays with custom start/stop indexes. But those that have custom indexes they can very easily expand/implement as helper class (for example array.indexFromLast(1) which means array[Length(array)]. This way you can have best of both worlds.

Surely if your language has custom indexes / ranges `Length(array)` is completely broken and the language provides something like "Index`Last" you can hook on? Because an array with indexes [3, 7) has length 4, but 4 is not the index of the last element.

Yup, correct. What I meant above with array[Length(array)] is for the languages that don't have it. Let me be more clear.

C/C++ doesn't have custom array indexes and as such is returning the last element of said array.

Delphi has custom array indexes and as such, taking your example with defining an array in the form , I would not get the last element in case of function as in to access example_array[7] element. Delphi also has function so you can iterate through a custom defined array by using keyword with the help of them. Option 2 would be to actually build my own helper (this is the most wanted case when you're dealing with multi-dimensional arrays that also have custom indexes) and I would have something like to access example_array[7] element.

Hope this cleared the confusion.

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

#162
Ruby’s symbols are undersold in terms of how useful they are and how they enable meta programming and DSL development. You can basically invent new language keywords with them.

    private def foo
      “bar”
    end
In this case, ‘def foo … end’ returns ‘:foo’, and ‘private’ is just another method that takes it as an argument and decorates the provided method. It’s not a special language keyword.

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

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

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.

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

#165
post #68

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

Then you have a problem with having to update both the constants module and the service that depends on it to change the service, and you need to handle packaging and distributing the module. Maybe if you have a monorepo where those issues are moot...

I didn't mean to suggest the constants module was a separate, reusable module, but a component of the same piece of software. By "module" here I meant "directory which can contain multiple importable files," so you could namespace your constants (constants/rfc_abcd.xyz, constants/customer_limits.xyz, etc).

I'd rather copy-paste any reused constants to different projects to avoid coupling, unless there was some kind of compelling domain/project specific reason.

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

#166
post #138
post #121

Earlier quoted context omitted.

Nowadays even Java will do that, var list = yourFunctionReturningThatSet();

Also on a function? var getDate() { return „no date“; }

No, but I think that was a good choice. In local code this is great, but in an interface contract being explicit about data type is a virtue.

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

#167
post #76

Python's `with` Function composition operators eg a |> b # Call `b` with `a` as an argument b Then you can do something like let x : Map = collect map(entries) |> flatten

I was under the impression that Elixir introduced this.

Elixir has forward pipes, but didn't invent them.

For instance Racket and Clojure have threading macros, which are more flexible as they're just macros (Clojure's `->` is equivalent to Elixir's pipe operator, but `->>` will fill in the last parameter rather than first, and `-->` lets you use a keyword to define where the parameter is inserted in each call).

Haskell let anyone who wants define their own pipe operator, historically you had to BYO, which wasn't exactly hard:

     (|>) = flip ($)
or

     x |> f = f x
would do (modulo fixity), but today it's provided by default as "(&)".

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

#168

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.

Is that giving you a number or a bytestring?

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

#169
kebab-case

enkebab–case

emkebab—case

These use dash, en dash and em dash, respectively. Most languages that allow you to use a decent amount of Unicode in variable names probably will accept that kind of kebab–case.

Those dashes look pretty similar to each other in monospaced fonts but not indistinguishable, so it’s readable and not super confusing. Might work. Why not?

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

#170
post #138

Earlier quoted context omitted.

Also on a function? var getDate() { return „no date“; }

No, but I think that was a good choice. In local code this is great, but in an interface contract being explicit about data type is a virtue.

For interfaces this would obviously not work (there is no implementation). But for class members it would.

I think this concept works very well in typescript and F#.

Post reply on HN