Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    A workaround for something like objects

    Coding
    2
    2
    221
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      petermeisenstein F last edited by

      I tried to make something like objects using structs maybe it can help you maybe its trash let me know

      1 Reply Last reply Reply Quote 0
      • Martin
        Martin Fuze Team last edited by Martin

        All, this is not a workaround for objects. It doesn't do what Peter thinks it does. But rather than just delete it, let me instead explain exactly what it DOES do because it will be a useful explanation for people.

        Code is shortened as I'm feeling somewhat lazy but it still demonstrates the topic

        function test()
            print("Hello World", chr(10))
            update()
            sleep(2)
        return void
        
        my_objects = [ .func = test() ]
        
        my_objects.func
        

        So let's look at the code. Ignore what's in the function, it doesn't matter, the key thing is that if you call it with 1 it prints something.

        At first glance it looks like the line myObj = [ .func = test(1) ] is storing a reference to the function which is then being called when you subsequently "call" it using myObj.func. But what is actually happening is that during the struct initialisation the method is called, it prints Hello World and returns void. At this point myObj.func equals void.

        Subsequently the statement myObj.func quite literally means and does nothing. In fact, you could be asking for trouble and enticing a crash. Hopefully this gets comepletely ignored by Fuze but I wouldn't chance it myself.

        You can prove this by repeating the line 3 times. You'll still only get Hello World printed once.

        For the record, storing function pointers in struct members is really a pretty advanced thing for a language to implement and it is not available in Fuze. No amount of wishing it will make it so I'm afraid.

        I'll leave this here for the explanation.

        1 Reply Last reply Reply Quote 3
        • First post
          Last post