gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:24:48 25 4
gpt4 key购买 nike

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++.

关于objective-c - Ada 支持 put_line 中的变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8993953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com