如何调用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
相关产品推荐
相关产品推荐

