• lime!@feddit.nu
    link
    fedilink
    arrow-up
    1
    ·
    4 months ago

    i think the most interesting design detail of lua tables is just glossed over as “nil-holes” in this article. namely, that nil values do not exist. there is no table.delete(key) method, you just zero out the value and the key stops existing. the same thing is true for any variable, if you set it to nil it ceases to be. i find that implementation fascinating.

    • pet the cat, walk the dog@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      4 months ago

      Except it’s ass if you want to do non-destructive data processing of arbitrary structures and your input and output might have null as a value. You can’t just know about fields a, b, and c of the table and leave everything else as it is, you need to know the whole structure and make sure you write null in the output for fields that have nil in them.

      Or, more realistically, use libraries that implement null as custom user data.

        • pet the cat, walk the dog@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          4 months ago

          Eh, dkjson implements null as an object with a metatable function that encodes it back as “null”. Hopefully it’s considered equal to itself in comparisons.

          Dkjson is fast enough for most scripting purposes. OTOH cjson’s userdata null is supported by some other libraries that deal with data structures.

          Of course, there’s a problem then that various libs may have their own nulls, not equal to each other. There’s even a lib that tries to marry some of them.

          • lime!@feddit.nu
            link
            fedilink
            arrow-up
            0
            ·
            4 months ago

            interesting! it should be equal since it’s always just a pointer to that same table.

            • pet the cat, walk the dog@lemmy.world
              link
              fedilink
              arrow-up
              0
              ·
              4 months ago

              I just never tried, so amn’t entirely sure.

              Iirc I needed a json lib on Windows, or was just fed up with compiling things for some reason, and used dkjson instead of cjson. It turned out to be more than adequate, as is pretty typical for Lua-only code. Although it can use the LPeg lib to speed up parsing.

    • thingsiplay@lemmy.ml
      link
      fedilink
      arrow-up
      0
      ·
      4 months ago

      I assume they did that for performance reasons, because removing entries is slow probably? From user perspective, it would have made it more sense to remove the key instead defining it as nil and then expecting the user to handle the nil. The key does not stop existing, right? I don’t program in Lua.

      • pet the cat, walk the dog@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        edit-2
        4 months ago

        Nil doesn’t exist as a value in Lua, because a variable or a field that don’t exist or are unassigned are indistinguishable from a nil value, since they all return nil when evaluating. In particular, this leads to the situation that you can’t have a table where some of the fields are assigned nil as the value, because those fields effectively don’t exist.

        • thingsiplay@lemmy.ml
          link
          fedilink
          arrow-up
          0
          ·
          4 months ago

          Ah I see, that makes it clear. I guess it is a “good enough” solution where it doesn’t matter in real world.

          • pet the cat, walk the dog@lemmy.world
            link
            fedilink
            arrow-up
            0
            ·
            4 months ago

            I’m guessing it’s more of a stylistic choice. Lisps typically work the same way, except they can retrieve the full map structure even if some fields contain nil as the value.

            Unfortunately, Lua’s approach hinders exchanging structures with null values with other environments: see my comment here.