News for hackers. Stuff that matters.
Re:Implementing UFCS for C++ in Clang by crazypython on Monday August 03, 2020 @07:38PM (#24044789)
Joining an iterable of strings into a single string. Python does:

  seperator.join(iterable)
  "".join(["foo", "bar", "hello"]) # => "foobarhello"
  ", ".join(["Hacker News", "Twitter"]) # => "Hacker News, Twitter
Many people have complained about this strange syntax:

https://mail.python.org/archives/list/[email protected]... https://mail.python.org/archives/list/[email protected]... https://mail.python.org/archives/list/[email protected]... https://mail.python.org/archives/list/[email protected]...

`["a", "b", "c"].join(iterable)` was rejected because it forces every iterable to implement iterable functions.

Additionally some iterables, like generators, are mutated when you iterate over them:

  ", ".join(streaming_api.fetch_names())
UFCS let's you write join as a free function and still use "".join(iterable)

NaN guarding:

  import std.traits;
  auto unwrap(T)(T input) if (isFloatingPoint!T) {
      // Asserts cost nothing when turned off with -check=assert=off
      // The compiler can fully inline this function, so it isn't expensive
      assert(input !== input.nan);
      return input;
  }
Use like this:

  if (objSize.unwrap < cellSize) {
     // Allocate a new cell for the object
  } else {
     // Insert into current cell
  }
The parentheses are omitted because Dlang has https://dlang.org/spec/function.html#optional-parenthesis. The above compiles into this:

  if (objSize.unwrap() < cellSize) {
     // Allocate a new cell for the object
  } else {
     // Insert into current cell
  }

There are no comments on this comment yet.