force type when using [] for property?

Get help with Spket IDE.
Post Reply
Gary
Posts: 18
Joined: Fri May 30, 2008 5:57 pm

force type when using [] for property?

Post by Gary » Wed Jun 25, 2008 9:16 pm

I'm in a rather unique situation. I'm using Spket to program for a non-web environment. This other environment has its own DOM which is basically a bunch of C objects exposed to the JavaScript runtime. In order to code for this other DOM, I have to use JS files to "recreate" those exposed C objects and then use those JS files in a profile. I've done pretty well so far but am having a rather hard time with the following.

JavaScript properties can be addressed with the dot (.) operator or [ ] as in the following:

Code: Select all

var a = { red : "apple", today : new Date( ) };
a["today"].|<-- cursor
... and the cursor position gives you the correct properties and methods for a Date object.

Now, in my make-shift DOM, I have some C style collection objects. They have an Item method to retrieve members of the collection and a Count propertiy to allow itteration. Each collection object is specifically typed... it can only hold ONE class of object. Here is a simple example:

Code: Select all

/** @class Value */
Value = function( ){ };

/** @class objA */
objA = { };

CollectionOfA = {
    /**
     * @param {Value} NameOrIndex
     * @return {objA}
     */
    Item : function(NameOrIndex) { return objA },
    /** @type {Number} */
    Count : 0
};
Because these objects are just placeholders for the Application DOM, they are static, don't do anything, and are only there to provide a coding framework. Since they are static, I can't get code completion to work when a developer uses the DOM framewok in a manner as such...

Code: Select all

/**
 * @param {Value} NameOrIndex
 * @return {Void}
 */
function doSomething(NameOrIndex) {
   var myObjA = CollectionOfA[NameOrIndex].| <-- cursor
   // do something with myObjA
}
Since the parameter passed is not a string representing a property of the collection, code completion does not know what the return value should be. For a collection object in this custom DOM, the return of [] is ALWAYS the stored type of the collection or a thrown error for not finding an item in the collection with the passed name or index. I'd like to "cheat" somehow... just not sure if there is a way. I'm guessing that there isn't since I'm stepping WAY outside the bounds of what is required for normal code work in a typical environment, but just incase someone reading has a trick up there sleeve I thought I'd ask.

Thanks,
-Gary

eric_suen
Site Admin
Posts: 678
Joined: Wed Sep 28, 2005 7:00 am
Contact:

Post by eric_suen » Thu Jun 26, 2008 8:14 am

The following code works, is that what you wanted?

Code: Select all

/** @class objA */
objA = { aaa : "" };

/**
 * @type objA[]
 */
var CollectionOfA;

var myObjA = CollectionOfA[NameOrIndex].|

Gary
Posts: 18
Joined: Fri May 30, 2008 5:57 pm

Post by Gary » Thu Jun 26, 2008 4:01 pm

Hey Eric, does that code really work for you? Perhaps something is wrong with my copy of the IDE...

Code: Select all

/** @class objA */ 
objA = { aaa : "" }; 

/** @type objA[] */ 
CollectionOfA = { Count : 0 }

CollectionOfA[0].|
... does nothing for me at the cursor location. I also tried just copy/paste the code snip you had in the post and got no code suggestions at the cursor either.

eric_suen
Site Admin
Posts: 678
Joined: Wed Sep 28, 2005 7:00 am
Contact:

Post by eric_suen » Thu Jun 26, 2008 4:09 pm

Code: Select all

/** @type objA[] */
var CollectionOfA;
works, but

Code: Select all

/** @type objA[] */
CollectionOfA = { Count : 0 }
not works

Gary
Posts: 18
Joined: Fri May 30, 2008 5:57 pm

Post by Gary » Thu Jun 26, 2008 5:15 pm

Eric,

Not sure why, but I can't get any code completion to work in these cases. I opened Spket with a new workspace, created an empty project with a single file and copied/pasted your code to make sure I wasn't miss-typing anything and it still doesn't work.

Regardless, assuming it should work (I'll try on a difference machine tomorrow... could be something goofy about this workstation?) I won't be able to use var for those collections. Remember, those things I'm creating, objA and CollectionOfA, are objects in my DOM representation that need to be available to a developer writing against the DOM in their own code. So, the collection and it's member types are all defined in external JS files brought into the project through ExtJS.

In those external JS files, I have to create a whole bunch of classes and, in one of the last JS files defined to ExtJS, I have to create objects out of those clases to provide the DOM framework. In the case of my collections (CollectionOfA in this thread) they are objects that belong to other objects in the DOM (Application is the parent object of the DOM) rather than variables in a developers code.

Thanks,
-Gary

eric_suen
Site Admin
Posts: 678
Joined: Wed Sep 28, 2005 7:00 am
Contact:

Post by eric_suen » Fri Jun 27, 2008 3:06 am

Try download the quick fix and replace the old one in your computer

http://www.agpad.com/downloads/com.spket.ui_1.6.12.jar
http://www.agpad.com/downloads/com.spket.js_1.6.12.jar

In preference Spket -> Editors -> JavaScript Editor, make sure option "Enable inline type hint for assignment statement" is checked.

Try following code

Code: Select all

/** @class objA */
objA = { aaa : "" };

CollectionOfA = /** @type objA[] */ {
};

CollectionOfA[1].|

Gary
Posts: 18
Joined: Fri May 30, 2008 5:57 pm

Post by Gary » Fri Jun 27, 2008 2:47 pm

Eric,

I can not thank you enough for putting time into this. Unfortunately, the quick fixes are not working. I downloaded the jar files and replaced the existing ones in the packages folder. The new option "enable inline" did appear on the preferences dialog, but has no effect. Perhaps there is another file in your system you have modified to address something else that changes the behavior? Just grabbing at straws.

For reference, I've tried this on 2 different systems. One is an XP machine with Sun Java 1.5.01 and the other is a Linux (Ubuntu) machine with Sun Java 1.6.0. If there is anything I can do to help debug this I would be more than happy to.

Thanks again,

-Gary

eric_suen
Site Admin
Posts: 678
Joined: Wed Sep 28, 2005 7:00 am
Contact:

Post by eric_suen » Fri Jun 27, 2008 3:19 pm

What is your default JavaScript profiles? you have to create a new JavaScript profile and set it as default, the profile provide by IDE like "FireFox 1.5" will not using this feature...

Gary
Posts: 18
Joined: Fri May 30, 2008 5:57 pm

Post by Gary » Fri Jun 27, 2008 4:06 pm

Ok, that was it. Using a custom profile caused the in-line typing to start working per the code sample. It looks like we are starting to combine two different threads... this one and my "Anyway to suggest..." (viewtopic.php?t=505) which dealt with in-line typing.

I played around a little with this and noticed the following.

Code: Select all

var x = /** @type subClass */ obj.getBaseClass( );
This effectively types as the subClass of the expected baseClass return. The only thing that would make this better (I know, I ask for the world) would be to allow you to re-type the variable again somehow... I put this in the other thread.

When using in-line typing for object creation, it kind of works but not quite. If you take the following...

Code: Select all

myCollection = /** @type someObject[] */ { };
... then myCollection is typed as an array with someObject as it's default content. The only problem with this is that myCollection is not an array in the case I'm trying to use it. Like I said, this is a unique case. The myCollection object simply an object where an unknown [<value>] defaults to a specific class. Having the array methods/properties appear for the myCollection is confusing, but the biggest problem is that, once typed as an array, myCollection looses any defined methods or properties.

Code: Select all

/** @class objA */
objA = { aaa : "" };

CollectionOfA = /** @type objA[] */ { Count : 0 };

CollectionOfA[1].| <-- gives property aaa (correct)

CollectionOfA.| <-- gives array properties, but no Count property (incorrect)
I'm thinking it would make sense to separate the in-line typing from what I'm trying to do here. Would it be possible to do something like the following:

Code: Select all

/**
 * @class CollectionOfA
 * @type {objA[]}
 */
CollectionOfA = { Count : 0 };
In this case, Collection of A is a defined object/class and the Code Completion would use the @type (or some other qualifier) to determine the object type of a [] when no specific property name or index is matched? Just a thought.

Thanks,
-Gary

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests