Live data from Hacker News

Microfeatures I'd like to see in more languages

buttondown.email

361–370 of 539 posts

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

#361
post #359

Absolutely damned is combination of Kotlin's several features: 1. Lambda functions can be defined with `{}`. 2.`foo(bar, somefunc)` is the same as `foo(bar) somefunc`. In other words, if the last parameter is a function, it can be provided AFTER closing parenthesis. 3. Interfaces that require only one method can be implemented on-side with a lambda function (i.e. `{}` syntax for no-param function). Combined those thr…

What exactly do you mean by "damned" here?

I too am confused by the unique use of this word

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

#362
post #190
post #32

Earlier quoted context omitted.

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…

You haven't understood the problem at all. No wonder, few people do any sort of bare metal programming. It's not about defining constants, or even writing code at all, it's about figuring out what this arbitrary piece of code is doing based on hardware datasheets . You search for constants defined in the datasheet in the piece of code you are analyzing to determine what it does, or where does it do specific things...

In my experience most (embedded) code is usually not possible to grep for such specific numbers anyway, because the assignments use bit-shift operators, set-macros, bitfields, binary literals, hex-literals non-hex-numbers, splitting a 16 bit number into a 2-element 8 bit array, mixing up the endianess, etc. An IDE that can find all assignments where right side has a numeric value of choice would cover more of such variants.

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

#363

I have long wondered why Ruby's symbols aren't in every language.

Ruby symbols are similar to keyword symbols in Common Lisp. There a keyword symbol is a symbol which evaluates to itself:

    CL-USER 69 > :a-keyword-symbol
    :A-KEYWORD-SYMBOL
Keywords with a similar name are identical:

    CL-USER 70 > (eq :a-keyword-symbol :a-keyword-symbol)
    T
One can't set keyword symbols to another value:

    CL-USER 71 > (setf :a-keyword-symbol 3)

    Error: Cannot setq :A-KEYWORD-SYMBOL -- it is a keyword.
They have certain features of normal symbols, like a property-list with keyword/value pairs.

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

#364

Earlier quoted context omitted.

>> Symbols > In Haskell you use hash has a prefix instead of colon. Can you give an example?

https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/over... You can do all sorts of things with them. Use them like symbols in Scheme, say to name fields `get #name user` or to access database tables, etc. But what's even more interesting is that the name is reflected up into the type. `get #name user` won't fail at runtime. Your database table name can be checked at compile time.

Ah, OverloadedLabels – I've only seen them in the `get #name user` use-case. I feel like Scheme/Lisp symbols are used quite a bit more generally, but maybe it's just not caught on yet in Haskell, also other features fill the same roles (e.g. in many lisps you can unquote a symbol and use it as the function of that name; people also often use them similarly to data constructors for pattern matching).

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

#365
post #4

My favorite is uniform function call syntax. In several languages (Nim, Koka, D, …), you can always write bar.foo(baz) instead of foo(bar, baz) and vice-versa. Another one from Nim is the implicit result variable. Instead of having to do this: func sum(nums: seq[int]): int = var result = 0 for num in nums: result += num return result you just do this: func sum(nums: seq[int]): int = for num in nums: result += num It…

> My favorite is uniform function call syntax. In several languages (Nim, Koka, D, …), you can always write bar.foo(baz) instead of foo(bar, baz) and vice-versa. To me, these are "Tell bar's foo to do something with baz." and "Tell foo to do something with bar and baz.". So being 'able' to flipflop the syntax is at least temporarily semantic'ly confusing.

I find the distinction between “foo” and “bar's foo” unnecessarily confusing. For example in C++, why is getting the last element of a “vector” something that belongs to it, but reversing a “vector” is something external?

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

#366
post #79

Earlier quoted context omitted.

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

You'd have to care about the difference between indexing with a literal and indexing with variables of different types/widths, and how indexing with a variable interacts with the size of the area. 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 ar…

In a language where arrays are fixed-size, I think the proper solution is to have arrays not indexed by integers, but by a custom modular type that depends on the array with values in [0,n) that allows literals in the range [-n, n-1], with literal ‘-1’ being a different way to write ‘n-1’, etc.

You’d need a way to get that type, for example as

  float a[10,20]          // two-dimensional array of floats
  typeof(a.dims(0)) i = 0 // modular type with values in [0,9]
  typeof(a.dims(1)) j = 0 // modular type with values in [0,19]
or, slightly neater:

  auto i = a.indextype(0)
  auto j = a.indextype(1)
Ugly syntax, but in a modern language, most code would probably do something like

  for (i,j,value) in a
where the types are inferred.

Having those modular types means the compiler would do the arithmetic correct for the array, while the negative literals allow programmers to specify “last” and “next to last” correctly.

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

#367
post #4

My favorite is uniform function call syntax. In several languages (Nim, Koka, D, …), you can always write bar.foo(baz) instead of foo(bar, baz) and vice-versa. Another one from Nim is the implicit result variable. Instead of having to do this: func sum(nums: seq[int]): int = var result = 0 for num in nums: result += num return result you just do this: func sum(nums: seq[int]): int = for num in nums: result += num It…

Initializing at what type and value?

The procedure's return type and its default (zero) value.

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

#368
Nested json dictionary lookups with default fallback, for scenarios where any of the sub-dictionaries could be null or missing a key. Write parseDateTime(config["a"]["b"]["c"]) safely as a oneliner. Haven't seen any language solve this in a neat way, without catching indexerror exceptions or ugly chaining of null-conditionals and empty dictionaries as fallback-values.

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

#369
post #79

Earlier quoted context omitted.

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

What 'mod' is for, innit.

Right, cause why let the language do it automatically when you can use a bug prone manual implementation?

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

#370
post #368

Nested json dictionary lookups with default fallback, for scenarios where any of the sub-dictionaries could be null or missing a key. Write parseDateTime(config["a"]["b"]["c"]) safely as a oneliner. Haven't seen any language solve this in a neat way, without catching indexerror exceptions or ugly chaining of null-conditionals and empty dictionaries as fallback-values.

It's not language-level, but I keep a Python class handy that wraps objects like that and has that behavior. You can also access items with config.a.b.c if desired (and if they're valid identifiers).
Post reply on HN