Array Operator Issue

#1

type Util {
xyz: function(quest: string, annotation: string): Type js server
}

And the function body looks like:

function xyz(quest, annotation) {
var newType = c3Type(quest);

if(newType !== null && newType.extensions !== null) {
    return newType.extensions()[annotation];
}

return null;

}

The function throws an exception that says ‘Javascript object cannot be converted to type Type’. If we change the return type of the function to Obj, then we get the error ‘Obj cannot be converted to type String’. If we change the function return type to string, we get out the object notation for the type: ‘[Object: obj]’.

The issue here is that the [] operator when accessing a C3 Type as a hashmap causes the resulting JSON object to be unrecognizable by the C3 Type System. When run in the console it works as expected. There are two solutions that need to be taken into account for us to be able to meet our future business needs. We need the extensions() object to be iterable, if I can call .forEach() on the object then that will greatly simplify my life. Additionally we need the [] operator on a type to work best in order for us to write our testing system generically.

Also, we aren’t sure why still the logs for this function didn’t appear in Splunk. My theory is the exception for the Type casting caused it to be interrupted or skipped?

0 Likes

#2

What are you actually trying to get back from this function? newType.extensions()[annotation] will not be an instance of Type, it will be an instance of whatever annotation type annotation is (e.g., Ann.Db if annotation is db).

0 Likes

#3

So the instance that we need is unknown until the function gets called. So we could need “db” or “myCustomAnn”, we don’t know that until invocation. Why in this case is the annotation not coerceable into the type Type? It fails to convert to type Obj as well, so what base type can we use to get data to return successfully?

0 Likes

#4

An annotation INSTANCE is an INSTANCE of a type. To return the type ITSELF you would return newType.extensions()[annotation].type().

That being said, passing around instances of the type Type is occasionally problematic. Very likely (if i’m understanding your use case) you should return the VALUE on the annotation, not the annotation itself. So return [string] or w/e by way of: return newType.extensions()[annotation].value;

1 Like

#5

newType.extensions()[annotation] is not coercible into a Type because it is not a Type, it is an instance of some annotation type.

Here is a ruby example of a similar paradigm:

> class User; end
=> nil
> u = User.new
=> #<User:0x00007fe747852bc8>
> u.is_a?(User)
=> true
> u.is_a?(Class)
=> false
> User.is_a?(Class)
=> true

Imagine Class is Type, User is myCustomAnn, and u is newType.extensions()['myCustomAnn']

1 Like