Wednesday, November 18, 2009

Generating getters and setters in Flex Builder

Fine print: This post discusses not so thoroughly tested regexes that the author used to edit his source code (and to fulfill his newly found affection for exploring regular expressions). Author cannot be held responsible or abused for the damages that these regexes might incur to your code and/or job. Eclipse has this cool feature that lets you generate getters and setters for the variables in your source code. Flex Builder 3, though based on Eclipse platform, doesn't have such an option. For those interested, here is a way to generate get and set methods for your ActionScript private/protected variables using the inbuilt Find/Replace and regular expressions. Though it assumes that you're using tabs for indenting the code (which is the default in FB), you can modify it for spaces easily. Open the Find/Replace window (Ctrl-F) and add the following regular expression to the Find field.

^((\t)+)(?:private|protected)\s+var\s+_(\w+):(\w+)\s*;\s*(\n)

Now add the following pattern to the Replace field.

$0$5$1public function set $3(value:$4):void$5$1{$5$1$2_$3 = value;$5$1}$5$5$1public function get $3():$4$5$1{$5$1$2return _$3;$5$1}$5$5

Don't forget to select the Regular expressions check box in the options box. The replace pattern doesn't accept any meta characters other than $, and hence we need to capture a tab and a new-line using the regex itself. Now use the Find, (Alt-N), Replace (Alt-R) and Replace/Find (Alt-D) to generate get and set methods for the required properties. If you want to generate get/set methods for all the private/protected variables in your class - and you're really courageous, use Replace All (Alt-A).

Tuesday, October 27, 2009

StackOverflow Reputation Tracker

Did I say that I've gotten addicted to StackOverflow? Here is a snippet of shell script to check your StackOverflow reputation from the comfort of your Linux console. Replace 165297 with your user ID.
#!/bin/sh 
json=$(curl -s http://stackoverflow.com/users/flair/165297.json) 
echo $json | sed 's/.*"reputation":"\([0-9,]\{1,\}\)".*/\1/' | sed s/,//
Haven't tested this for other sites in the trilogy, but I believe this would work for all the three sites and also the meta. Now onto writing an AIR app that would track reps and alert about new questions with your favorite tags... :)

Friday, April 3, 2009

VerifyError 1102

VerifyError: Error #1102: Illegal default value for type * If you ever get this rather unusual run-time error, here's what to do. If you are using default values for arguments in a method's declaration part, make sure that the types match well. For instance, I wanted to explicitly call an event handler from the code and hence used null as the default value for the event parameter - only I typed it false instead of null (yeah, its Friday evening here). I don't know why the compiler thinks its okay to assign false (or a String or AnyThing) to an Event variable. private function init(e:Event = false):void So check your code for something silly like this, fix it and you are good to go. This run-time error is strange because livedocs says that "The VerifyError class represents an error that occurs when a malformed or corrupted SWF file is encountered". So you conclude that the SWF is having its issues on the Friday evening and rebuild the project to fix it. But no matter what you do to fix the "corrupted" SWF, you will get the same error again and again. Also, Flex doesn't show the corresponding MXML/AS file name or the line of code that caused this error, which it normally does with errors. It in fact traces the call stack, but that's not quite useful. One would normally expect the bug to be in the top most method of the call stack or in a method called from that. But here it seems that the top most method in the stack trace is the method that accesses any method/property from the buggy class for the first time in application. Adobe's documentation on Flex run-time errors says that "ActionScript in the SWF is invalid. If you believe that the file has not been corrupted, please report the problem to Adobe." - not really helpful either. But fortunately this is not even a run-time error - or this wouldn't have been one, had the compiler done its job well. And it turns out that the error message - Illegal default value for type * - is in fact correct. Compiler will detect wrong assignments in the case of basic types. For instance, if it was function(t:String = 3) you will get a compiler error 1184: Incompatible default value of type int where String is expected along with file name and the exact line of code that went wrong. But if its anything other than basic types, compiler just ignores it and you get this scary "malformed SWF". Hope this helps someone.
Links added with LinkIt

Wednesday, February 25, 2009

Hello World

This, ladies and gentlemen, would be my attempted English blog. (I am attempting one in Malayalam here). Be warned that this is the first time I am writing anything completely by myself in English - other than exam papers and assignments, of course. Err, I can't really make that claim - quite a few of them were actually not completely written by myself ;) The first question that I had upon deciding to start this blog was what would I write about, and unfortunately that still remains unanswered. Normally new bloggers complain about not having anything to write about after few posts; but I'm doing that in my very first post itself - quite impressive, isn't it? I guess I would start by jotting down some FYIs about Flash/Flex that I had to learn the hard way.
Links added with LinkIt