objective-c - Ada 支持 put_line 中的变量吗?

Ada 是否支持类似于字符串中 Obj-C 变量的东西?

NSLog(@"This is text, here's a variable %f", floatvar);

我想写出漂亮的单行代码,例如:
put_line("The answer is %v", answer);

代替

put_line("The answer is ");<br/> put(answer);

最佳答案

您可能喜欢 Ada 常见问题解答,特别是 part 9.9 .为了完整起见,我在这里引用它:

While the standard package Text_IO provides many features, the
request for a printf-like function is not unusual.

(solution based on a suggestion by Tucker Taft)

It is possible to produce a printf-like capability by overloading the "&" operator to take an object of type Format and an object of some type and return the Format, properly advanced, after having performed the appropriate output. The remaining format can be converted back to a string--e.g. to examine what is left at the end of the format string-- or simply printed to display whatever remains at the end. For example:

 with Text_IO;
 package Formatted_Output is
   type Format is
     limited private;

   function Fmt (Str : String)
     return Format;

   function "&" (Left : Format; Right : Integer)
     return Format;
   function "&" (Left : Format; Right : Float)
     return Format;
   function "&" (Left : Format; Right : String)
     return Format;
   ... -- other overloadings of "&"

   procedure Print (Fmt : Format);

   function To_String (Fmt : Format)
     return String;

 private
   ...
 end Formatted_Output;

 with Formatted_Output; use Formatted_Output;
 procedure Test is
   X, Y : Float;
 begin
   Print (Fmt("%d * %d = %d\n") & X & Y & X*Y);
 end Test;

The private part and body of Formatted_Output are left as an exercise for the reader ;-).

A "File : File_Type" parameter could be added to an overloading of Fmt if desired (to create something analogous to fprintf).

This capability is analogous to that provided by the "<<" stream
operator of C++.

https://stackoverflow.com/questions/8993953/

相关文章:

matplotlib - 如何更改 matplotlib 3D 图轴的颜色?

asp.net-mvc - 我怎样才能通过 LCID 获得文化

objective-c - 在 objective-c 中获取颜色分量

css - 使用 CSS 选择类下面的类

php - SimplePie 可以从提要中抓取图像吗

mercurial - 替换 hg 分支的内容

php - FOS 用户包安装错误

asp.net-mvc-3 - MVC 3 POST 数据和 Id 字段

python-2.7 - Python None 如果检查失败

google-maps - 谷歌地图标记仅在 iPhone/iPad 上在特定缩放级别消失