ASL: Better internationalization and foreach support

I’ve improved ASL’s internationalization support recently by being geared to C++ Boost libraries and its format library.
ASL does now contain struct type “AFormat” which allows you to add arguments to strings by using shortcut methods like “s” which means string or “r” which means real.

Since it would be very annoying to create an “AFormat” object and destroying it each time you want to format a string I’ve written the global function “Format” which creates an object automatically.
For releasing it in the same line you could call the method “result” which returns the formatted string and destroys the format object (similar to Boost libraries I think).
The ASL did already have some string formatting support by providing functions like “IntegerArg” and “RealArg” but since there are no parameter types like “…” (C/C++) for unlimited arguments it was nearly unreadable when formatting strings by adding many arguments.
Additionally I had an implementation which did only allow user to specify his format arguments by using C-like expressions: %r, %i etc.
“AFormat” allows you to specify it like in Boost library or Qt, by its order: %1%, %2% etc. since it stores internal argument position.
Furthermore I’ve added support for various native types of Jass like handles, units, players etc.
Here’s a simple comparison example for formatting strings with ASL:

local string result0 = StringArg(StringArg(IntegerArg(tr("You're %i years old and you're called %s. Besides you're %s."), 0), "Peter"), "gay")
local string result1 = Format(tr("It's %1% o'clock and you're going to die in %2% hours. Maybe you should ask %3% why you're still alive.")).t(GetTimeOfDay()).i(3).s("Peter").result() // t means time and can be used for formatting time automatically (real values)
local string result2 = String(Format(tr("In the name of %s")).p(Player(0))) // You can also use the global String function (like in Boost libraries). p means player and adds player's name.

Another new feature of ASL is the foreach loop. Many programming languages do have native foreach support. C++ does not have native support but libraries like Boost and Qt do have preprocessors which generate them automatically for specific types of containers.
Since vJass allows you to define text macros this can be done with it, too. Surely it doesn’t look as nice as in C++ since you have to call text macros by the statement “//! runtextmacro” and you can not declare local variables anywhere but it’s a funny and nice feature, although:

local AIntegerList list = AIntegerList.createWithSize(10, 12)
local integer value
//! runtextmacro A_FOREACH("list")
debug call Print("Value " + I2S(aIterator.data())) // Use global iterator "aIterator".
call aIterator.setValue(GetRandomInt(0, 100))
//! runtextmacro A_FOREACH_END()

//! runtextmacro A_FOREACH_2("value", "list", "AIntegerListIterator")
debug call Print("Value " + I2S(value))
//! runtextmacro A_FOREACH_END()

Unfortunately there is a vJass bug when implementing an interface in size limited structs. Otherwise you probably could remove the iterator type argument.
Note that since “aIterator” is global you should not use “TriggerSleepAction” calls in foreach loops.
Maybe I will add a third version which takes an iterator as argument but usually you could write it commonly if you want to use your own iterator:

local AIntegerListIterator iterator = list.begin()
loop
exitwhen (not iterator.isValid())
debug call Print("Value " + I2S(iterator.data()))
call iterator.next()
endloop
iterator.destroy()

At the moment foreach loops can only be used on map and list containers. Vector container does not support iterators, yet.
I will release ASL version 1.2 next months I hope because there had been made lots of changes and fixes lately.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: