nil

Home Forums General Programming nil

Tagged: 

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #72236
    human fly
    Participant
      • Topics: 124
      • Replies: 1070
      • Total: 1194
      • ★★★★

      if blabla ~=nil

      i remain a bit hazy on ‘nil’ and have been managing to
      bypass learning more about it. any chance of a ‘layman’s
      explanation’? 🙂 i find these tutorials a little bit
      opaque on some of the finer points needing most attention:
      https://www.lua.org/pil/2.1.html
      and then this(@2.3):
      https://www.lua.org/manual/5.1/manual.html

      (forgive me, i *am* actually reading up on this as
      i post …)

      ie: when you declare with blabla = nil
      or, when it’s if ~=, in a condition
      (and any other common applications)

      #72237
      human fly
      Participant
        • Topics: 124
        • Replies: 1070
        • Total: 1194
        • ★★★★

        google searches do not return great info for this.
        best i could find so far:
        https://www.gnu.org/software/emacs/manual/html_node/eintr/nil-explained.html

        (methinks a list of the best Lua sites somewhere on this forum
        would not be a bad thing)

        still trying to get my head round it:
        when you declare new variables, you can assign nil: blabla = nil
        and that means .. it has NO value, but that you’ve created a
        space to store a value in. correct?

        what happens if you don’t do that ? if you just declare
        your variable? is it for local or global, or both?
        https://www.lua.org/pil/2.1.html
        here, it says:
        ‘a global variable has a nil value by default, before a first assignment, and you can assign nil to a global variable to delete it.’

        thus, i’m finding it a bit tricky to grasp.

        #72238
        Puppeteer
        Participant
          • Topics: 16
          • Replies: 185
          • Total: 201
          • ★★

          LUA is a bit weird in this instance. Variables are global by default, unless you specifically declare them as local.

          According to the LUA documentation you don’t need to explicitly declare a variable to use it. LUA assumes that every single possible variable is declared. nil is a special value and essentially it marks the variable as empty.

          In many cases if you try to do operations on a variable with a value nil you will generate errors, so having checks, such as the one above, or setting variables to initial values is good practice, but programmatically is not strictly required.

          The Puppeteer
          http://godlike.com.au

          #72239
          human fly
          Participant
            • Topics: 124
            • Replies: 1070
            • Total: 1194
            • ★★★★

            take this example (all i can think of right now):

            multiple if syntax question

            post#2: dasfaker suggests declaring local variables as:

            local p1 = nil
            local p2 = nil
            local p3 = nil
            local p4 = nil  
            local sum = nil
            if panel:getModulatorByName("P1"):getValue() == 0 then p1 = 0 else p1 = 1 end
            if panel:getModulatorByName("P2"):getValue() == 0 then p2 = 0 else p2 = 1 end
            if panel:getModulatorByName("P3"):getValue() == 0 then p3 = 0 else p3 = 1 end
            if panel:getModulatorByName("P4"):getValue() == 0 then p4 = 0 else p4 = 1 end
            
            sum = (p1*1) + (p2*2) + (p3*4) + (p3*8)
            panel:sendMidiMessageNow(etc)

            so i tried different versions: this way, and having
            variables as global (“P1″/”P2″/”P3″/”P4”) without assigning nil.

            is it maybe because they are local and their value isn’t mentioned
            elsewhere, -and the fact that it doesn’t do ie:
            p1=panel:getModulatorByName("P1")
            so the local ‘p1’ isn’t yet associated with the modulator ‘P1’

            what i ended up doing was:
            local p1 = panel:getModulatorByName("Partial1_OnOff_Pt1"):getModulatorValue()

            so i suppose i didn’t need to create a value by assigning nil there.

            as for ~=nil : so i can interpret that as ‘if (blabla) has a value’ ?

            is that a bit of a dummy condition, just so you can put ‘then’
            after it?

          Viewing 4 posts - 1 through 4 (of 4 total)
          • The forum ‘Programming’ is closed to new topics and replies.
          There is currently 0 users and 66 guests online
          No users are currently active
          Forum Statistics
          Threads: 2,495, Posts: 17,374, Members: 77,605
          Most users ever online was 12 on January 22, 2019 3:47 pm
          Ctrlr