How does "any" type work with basic types like string?

118 views Asked by At

I am trying to use a switch clause to define what to do depending on the real type of a variable of type "any", my code is crashing. The code is the next:

handleResponse(any.parseType("string",respValuesRaw[type]), currEPIFace);
...
...
...
action handleResponse(any response, CurrentExtraParamsInteface currEPIFace){

switch(response){
    case string:
        {}

The error I am getting is: "ParseException - Error in ParseType() method: Unable to parse string: missing opening quote"

However, the respValuesRaw variable is a dictionary of type <string,string>

This is on Apama 10.1.

Any idea of what could be wrong?

3

There are 3 answers

0
Tony_craft On BEST ANSWER

I found out this kind of parsing is not ment to be used with basic types, so I changed the way I call the handleResponse action:

handleResponse("string", currEPIFace);

Actually, any string value will fit.

0
Chris Reed On

As per the doc for any.parseType, this is equivalent to calling type.parse, so this is equivalent to string.parse, which states:

The parse method takes a string in the form used for event files. String arguments must be enclosed in double quotes. All escape characters will be converted to the natural character.

If you just want to use the value of the entry of the dictionary, you probably want to just write:

handleResponse(respValuesRaw[type], currEPIFace);

The dictionary entry's value is a string, and it's legal to pass any type to an 'any' parameter.

0
Saurav Sahu On

Assigning a basic type like string to any type is absolutely legal. Problem is somewhere else.

Since you're not passing a string in the form used for event files, it is giving error. Decoding the error message becomes really simple once you look at one example where parseType method is used. That gives some hint why does it really look for the opening quote in the argument.


Your problem simply put:

package com.apama.test;

event Evt{}
monitor Foo {
    action onload() {
        Evt e1;
        // handleResponse(any.parseType("string", "World!")); // #1 Invalid argument. Doesn't work
        handleResponse(any.parseType("com.apama.test.Evt", "com.apama.test.Evt()")); // #2
        handleResponse("World!"); // #3
    }
    action handleResponse(any response){
        log "Hello " + response.toString() ;
    }
}

prints:

com.apama.test.Foo [1] Hello any(com.apama.test.Evt,com.apama.test.Evt())
com.apama.test.Foo [1] Hello any(string,"World!")

while uncommenting #1 gives error as shown below:

ParseException - Error in parseType() method: Unable to parse string: missing opening quote 

Further, if you pass a properly-formed-but-a-non-existing-event to parseType method, it will throw an error stating that the type couldn't be found.

ParseException - Error in parseType() method: Unable to find type 'com.apama.test.Evt2'