You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调用Ballerina中定义为record字段的函数?

问题分析与解决

错误信息翻译

原错误:function invocation on type 'SourceInteraction' is not supported
中文翻译:不支持对类型'SourceInteraction'进行函数调用

问题根源

你定义的公共函数名write,和SourceInteraction record里的字段名write完全重复,导致编译器混淆了你的意图——它误以为你是想把SourceInteraction实例当作函数直接调用,而非访问实例的write字段。

解决方案

有两种方式可解决该命名冲突:

方案1:重命名公共函数

修改公共函数名称,避开和record字段的同名冲突,比如改为invokeSourceWrite:

public function invokeSourceWrite(SourceInteraction interaction, string id, anydata data)
        returns error? {
            
    check interaction.write(id, data);
}

方案2:明确指定访问record字段

用@符号明确标注要访问的是record字段,消除命名歧义:

public function write(SourceInteraction interaction, string id, anydata data)
        returns error? {
            
    check interaction.@write(id, data);
}

内容的提问来源于stack exchange,提问作者Tharmigan Krishnananthalingam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 08:13:11