ChatGPT - How Long Till They Realize I’m a Robot?

Image
I tried it first on December 2nd... ...and slowly the meaning of it started to sink in. It's January 1st and as the new year begins, my future has never felt so hazy. It helps me write code. At my new company I'm writing golang, which is new for me, and one day on a whim I think "hmmm maybe ChatGPT will give me some ideas about the library I need to use." Lo-and-behold it knew the library. It wrote example code. It explained each section in just enough detail. I'm excited....It assists my users. I got a question about Dockerfiles in my teams oncall channel. "Hmmm I don't know the answer to this either"....ChatGPT did. It knew the commands to run. It knew details of how it worked. It explained it better and faster than I could have. Now I'm nervous....It writes my code for me. Now I'm hearing how great Github Copilot is - and it's built by OpenAI too...ok I guess I should give it a shot. I install it, and within minutes it'...

Adobe Flex 3.0 - Passing parameters to event listeners - actionscript

So I just started this job, and I'm writing an application that lets users view photos and tag them, giving descriptions of the tags as well.  I was trying to add an event listener to a Text object, when I realized there was no way to add a parameter to it!  Because the addEventListener function goes like this:

var itemToPass:Object;
textObject.addEventListener(MouseEvent.MOUSE_OVER, callbackListenerFunction);

there was no way to pass a parameter, except for the event object, which is passed automatically.  Of course you could use an anonymous function for the callback, like this:

var itemToPass:Object;
textObject.addEventListener(MouseEvent.MOUSE_OVER, function(event:Event):void {
    callbackListenerFunction(itemToPass);
});

And this will work in many cases.  But the underlying problem is still there!  You see, if you change the value of itemToPass later on in the current code block, then the value that's passed into callbackListenerFunction is ALSO changed!  The code I had at first was something like this:


for each(textObj:Text in TextObjectList)
{
    //..........
    var itemToPass:Object = someValue * whatever;
    textObject.addEventListener(MouseEvent.MOUSE_OVER, function(event:Event):void {
        var param1 = itemToPass;
        callbackListenerFunction(param1);
        //................
    });

}

which may seem like a good solution to the untrained eye....or brain...whatever.  The problem is the way Flex 3.0 (or actionScript, I dunno) instantiates and stores variables.  You see, in most object-oriented languages, when you instantiate a variable, it's value is only available within the block of code where it was declared, and if it's declared in a loop, then it gets garbage collected and then re-instantiated every loop through. BUT Flex 3.0 doesn't work that way.  In Flex, when you instantiate a variable, it's available for the rest of that entire function or class where it was declared, and if it's declared in a loop, then the variable stays the same, but it's value may change.  So, in the code just above, I was passing the exact same value to all the listener functions for all the Text objects! - Whatever value itemToPass has on the last loop execution.  The solution is to put whatever's inside the loop in it's own function.  That way, every time the loop executes, a new function is started and new variables are instantiated.  Here's the code:


for each(textObj:Text in TextObjectList)
{
    //..........
    helperFunction(someValue:int, whatever:int);
}

private function helperFunction(someValue:int, whatever:int):void {
    var itemToPass:Object = someValue * whatever;
    textObject.addEventListener(MouseEvent.MOUSE_OVER, function(event:Event):void {
        var param1 = itemToPass;
        callbackListenerFunction(param1);
        //................
    });
}

So there ya go.  If you ever have an issue like that again, you know where to look for the solution.

Cheers!

Comments

Post a Comment

Popular posts from this blog

ChatGPT - How Long Till They Realize I’m a Robot?

My experience with Udacity

Architectural Characteristics - Transcending Requirements