【HarmonyOS NEXT+AI】问答09:仓颉编程语言异常处理macro is expanded
the error occurs after the macro is expanded 这里就这位学员的问题,统一做下回复,以方便其他同学参考。往期问答,可以在我主页查到。 问题定位 hvigor Finished :entry:default@GenerateCangjieResource. . . after 7 ms> hvigor ERROR: Failed :entry : default@compileCangjie. . . hvigor ERROR: Tools execution failed. error: undeclared identifier 'percent' ==> D:\CangJie\AIAssistant[entny\src\main\cangjie \util\chatline.cj:81:80: 81 l }.padding(4) .backgroundColor(OXFFFFFF).constraintSize(maxWidth: 90.percent) .borderRadius(10) l note: which is expanded as followsl l/ 73.13 / Row() { thisView.observeComponentCreation({ elmtId,isInitialRendep => Row() { thisView.obser l note: the error occurs after the macro is expanded ==> D:\CanqJie\AIAssistant \entny\snc\main\cangjie\util\chatline.cj:73:1: 从错误信息可以看到the error occurs after the macro is expanded是指当前环境没有提供宏。 那么什么是宏呢? 在仓颉中,宏可以理解为一种特殊的函数。一般的函数在输入的值上进行计算,然后输出一个新的值,而宏的输入和输出都是程序本身。在输入一段程序后,输出一段新的程序,这段输出的程序随后用于编译和执行。为了把宏的调用和函数调用区分开来,在调用宏时需使用 @ 加上宏的名称。 从报错的位置可以知道,报错代码是这个: Column() { Text(title).fontSize(24).fontWeight(FontWeight.W400) .width(100.percent).height(100.percent) .textAlign(TextAlign.Center) }.width(100.percent).height(48) .alignItems(HorizontalAlign.Start).backgroundColor(0x5DE2E7) 这里的100.percent就是使用了宏。那么要宏就要导入宏。 解决方法 在代码中导入这个即可 import ohos.base.LengthProp 参考引用 更多仓颉学习资料,详见:
在“HarmonyOS NEXT+AI大模型打造智能助手APP(仓颉版)”课程里面,有学员在开发国产中遇到了这么一个问题:


《跟老卫学HarmonyOS开发》:https://github.com/waylau/harmonyos-tutorial
《跟老卫学仓颉编程语言开发》:https://github.com/waylau/cangjie-programming-language-tutorial
“HarmonyOS NEXT+AI大模型打造智能助手APP(仓颉版)”:https://coding.imooc.com/class/927.html
《仓颉编程从入门到实践》(北京大学出版社):https://waylau.com/about-cangjie-programming-language-tutorial-book/















