简体   繁体   English

如何在 AssemblyScript 中编写 printf() function?

[英]How do I write a printf() function in AssemblyScript?

I mostly need this for logging where I need to pass in arbitrary arguments (ints floats, objects).我主要需要这个来记录我需要传入任意 arguments(整数浮点数,对象)的地方。

One solution is to write一种解决方案是写

let i:i32 = 1;
let f:f32 = 1.1;
log ("Message "+i.toString()+" "+f.toString())

This is very awkward and verbose to write.这写起来非常笨拙和冗长。

You can also have multiple log functions, again awkward还可以有多个日志功能,又尴尬了

log_i (msg:string, i:i32);
log_i2 (msg:string, i:i32, i2:i32);
log_f (msg:string, f:f32);
etc

Seems like you cannot have a generic array that holds i32, f32 and objects at the same time.似乎您不能拥有同时包含 i32、f32 和对象的通用数组。 So not even sure how to pass in varargs.所以甚至不确定如何传入可变参数。 Maybe I can box them but it is again awkward without auto-boxing.也许我可以装箱,但没有自动装箱又很尴尬。

What would be a good solution for this straightforward usecase?对于这个简单的用例,什么是好的解决方案?

Simply use Typescript style Template strings.只需使用 Typescript 样式模板字符串。

log (`Message ${i} and ${f}.`)

Assemblyscript will automatically generate the toString() and string concatenate statements. Assemblyscript 将自动生成 toString() 和字符串连接语句。

  1. Simple and concise简洁明了
  2. More expressive logs rather than put all arguments at the end.更具表现力的日志而不是将所有 arguments 放在最后。
  3. No awkward function calls, varargs, etc.没有尴尬的 function 电话、可变参数等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM