【问题标题】:How does "any" type work with basic types like string?“任何”类型如何与字符串等基本类型一起使用?
【发布时间】:2019-03-14 10:48:20
【问题描述】:

我正在尝试使用 switch 子句根据“any”类型的变量的实际类型来定义要做什么,我的代码正在崩溃。代码如下:

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

switch(response){
    case string:
        {}

我得到的错误是:“ParseException - ParseType() 方法中的错误:无法解析字符串:缺少开头引号”

但是,respValuesRaw 变量是 <string,string> 类型的字典

这是在 Apama 10.1 上。

知道什么地方出了问题吗?

【问题讨论】:

    标签: apama


    【解决方案1】:

    根据any.parseType的文档,这相当于调用type.parse,所以这相当于string.parse,其中指出:

    parse 方法采用用于事件文件的格式的字符串。 字符串参数必须用双引号括起来。全部逃离 字符将被转换为自然字符。

    如果你只想使用字典条目的值,你可能只想写:

    handleResponse(respValuesRaw[type], currEPIFace);
    

    字典条目的值是一个字符串,将任何类型传递给'any'参数都是合法的。

    【讨论】:

      【解决方案2】:

      any 类型分配一个基本类型如字符串是绝对合法的。问题出在其他地方。

      由于您没有以用于事件文件的形式传递字符串,因此会出错。一旦您查看一个使用parseType 方法的示例,解码错误消息就变得非常简单。这给出了一些提示,为什么它真的在论点中寻找 开场白


      简单地说你的问题:

      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() ;
          }
      }
      

      打印:

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

      取消注释#1 时会出现如下所示的错误:

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

      此外,如果您将格式正确但不存在的事件传递给 parseType 方法,它将抛出一个错误,指出找不到类型。

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

      【讨论】:

        【解决方案3】:

        我发现这种解析不适用于基本类型,所以我改变了调用 handleResponse 动作的方式:

        handleResponse("string", currEPIFace);
        

        实际上,任何字符串值都适合。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-01-07
          • 2022-08-19
          • 1970-01-01
          • 1970-01-01
          • 2021-07-10
          • 2021-04-14
          • 1970-01-01
          相关资源
          最近更新 更多