Hacker Newsnew | past | comments | ask | show | jobs | submit | okla's commentslogin

Mach engine is written in Zig


I wonder if this can be emulated with for loop:

  for(FILE *fp = fopen(path, "wb");
      fp != NULL;
      fclose(fp), fp = NULL) {
    ...
  }


I'm glad you suggested that, because one of the first comparisons I drew in my article[1] about the "with"-syntax was how it looks like a for-loop.

If you are interested, I did provide a few macros[2], but as another commenter pointed out, they won't handle an early return. You can use "break" to exit early though:

  #define with(declare, startup, cleanup, block) \
      {                      \
          declare;           \
          if (( startup )) { \
              do {           \
                  block;     \
              } while(0);    \
              cleanup;       \
          }                  \
      }

  with (FILE *fp, fp = fopen(path, "rb"), fclose(fp), {
    fread(buf, 1, sizeof(buf)-1, fp);
    if (ferror(fp)) {
      perror(path);
      break;
    }
    printf("%s\n", buf);
    success = 1;
  })
There's also "withif"

  #define withif(declare, startup, cleanup, block, otherwise) \
      {                      \
          declare;           \
          if (( startup )) { \
              do {           \
                  block;     \
              } while(0);    \
              cleanup;       \
          } otherwise;       \
      }
[1]https://github.com/taeber/cwith

[2]https://github.com/taeber/cwith/blob/master/with.h


No, because exiting the block early (e.g., with return) won't run the cleanup code.


That's a little bit like saying, it's impossible to write functioning code because you could make a mistake. But an abstraction that can be misused might still be an abstraction worth using.

Here is a macro that can be used to emulate a defer

    #define CONCAT_(a, b) a ## b
    #define CONCAT(a, b) CONCAT_(a, b)
    #define UNIQUENAME() CONCAT(i_, __LINE__)

    #define SCOPE_(counter, init_stmt, exit_stmt)  for (int counter = ((init_stmt), 1); counter--; (exit_stmt))
    #define SCOPE(init_stmt, exit_stmt) SCOPE_(UNIQUENAME(), (init_stmt), (exit_stmt))
Granted it's a hack, but it can be useful at times. I've used something like it to define a large data hierarchy in code for example, as having to close all the nodes manually is tedious.

You could wrap a second for-loop around the definition of the macro to at least be able to catch misplaced "break" statements.


Looks very interesting. Can you please provide usage example?


Sure, you'd code like

    SCOPE(Resource *ptr = acquire_resource(),
          release_resource(ptr))
    {
        // do stuff with resource ptr.
    }
Actually, to allow variable declarations in the init_stmt like above, you'll need to use two nested for-loops:

    #define SCOPE_(name, begin_stmt, end_stmt) for (int name = 0; !name; assert(name && "should never break from a SCOPE")) for (begin_stmt; !name; name++, (end_stmt))
It is natural to add another layer of specific usage macro like this:

    #define UI_NODE(ctx) SCOPE(push_ui_node(ctx), pop_ui_node(ctx))

    UI_NODE(ctx)
    {
        ui_color(ctx, UI_COLOR_RED);

        const char *items[3] = { "a", "b", "c" };
        for (int i = 0; i < 3; i++)
        {
            UI_NODE(ctx)
                ui_text(ctx, items[i]);
        }
    }
Naturally, if you're already on C++, better code these macros in terms of RAII instead of abusing for-loops. That will add a little robustness.


I'm on C89, but this is a very useful trick. Thanks!


What is completely broken for me since macOS 11/iOS 14 update is iCloud tabs. They simply don't sync between Mac and iPhone anymore, the list is almost always empty. Sometimes it contains some random out-of-date links, but touching any of them make them all disappear.


It was called web-surfing back in the day. Sadly, we don't surf anymore.


I was always wondering – isn't it better to make usage of voice assistant similar to simple phone call? Why it should be in speaker mode?


I would assume from a UX perspective, the movement from handset phone back to smartphone screen is seen as an inconvenience and interrupts the flow of the process. Add in that many phones have screen conditions tuned to what context the phone is being used, you increase the chance of interrupting the process when the mechanisms within the phone misinterpret the phone's current position and orientation. (e.g., iOS screens enter a "call mode" when held up to the ear, or screen rotation will change what options are available. Even on new iOS and Android devices, sometimes the OS just seems to get "stuck" in a particular orientation and only after considerable fiddling with the phone will it rotate to the intended orientation)

Likely, designers made the call that they could just avoid the entire mess by operating the entire voice assistant in speaker mode instead of trying to compete with other UX/UI features.

Additionally, I would assume that the idea of simply talking aloud to a device replicates the Sci-Fi computer experience for computer interaction, most notably in Star Trek in which crew members simply summoned the computer with "Computer: [plain language command or query]". Even outside of Star Trek, this was a fairly common idea for interaction with futuristic computer systems, and for enough of the population, this method of interaction is part of the cultural memory.


Siri used to have "raise to talk" that would activate Siri automatically if you put the phone to your ear outside a phone call, but from some googling it was removed in an update.


If you activate Google's voice recognition from a bluetooth headset, it will respond to you through the headset -- but you often have to interact with the device anyway, to get it to do more than the most simple of things.


Actually next time just try talking to it to complete the task.

I use Google's voice recognition a ton when on my motorcycle with a Bluetooth helmet. I've gotten in the habit of just trying things and to my surprise many have worked.

Play/pause music, mute/unmute voice navigation, ask how long until my next turn, ask it to navigate to somewhere else, call/text/send a hangouts message to a contact by their nickname, play a specific band or album of music, etc...


You also have to be wearing a bluetooth headset, which is as much of a social faux pas as talking to siri in public.


I have Bluetooth headphones, which I'm wearing at the moment as I'm listening to music. They're pretty good at the headset thing too.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: