Greetings all,
Today I discovered the "typedef" keyword in the ycp langauge. I'd not
realised this existed because I hadn't seen it in any documentation,
or used in modules. It seems it's very occasionally used. Is there any
reason for this? It seems to provide a mechanism to avoid having to
deal with lists of maps of lists all over the place. The typedefs
can't be imported out of a module, but one can work around this by
"including imports". See example.
// Test.ycp
{
include "Car.ycp";
include "CarBrand.ycp";
Car car = Car::newInstance();
y2milestone("New car speed is %1",Car::getSpeed(car));
Car::setSpeed(car,100);
y2milestone("Now car speed is %1",Car::getSpeed(car));
y2milestone("This car is a %1",Car::getBrand(car));
Car audi = Car::newBranded(CarBrand::AUDI);
y2milestone("This car is a %1",Car::getBrand(audi));
}
// output
2007-12-03 18:00:06 <1> padd(5080) [YCP] yast/Test.ycp:6 New car speed is 0
2007-12-03 18:00:06 <1> padd(5080) [YCP] yast/Test.ycp:8 Now car speed is 100
2007-12-03 18:00:06 <1> padd(5080) [YCP] yast/Test.ycp:10 This car is a `FORD
2007-12-03 18:00:06 <1> padd(5080) [YCP] yast/Test.ycp:12 This car is a `AUDI
// modules/car.ycp
{
module "Car";
include "CarBrand.ycp";
include "Car.ycp";
global Car newInstance()
{
Car car =
$[
"speed":0,
"brand":CarBrand::FORD,
"colour":"red"
];
return car;
}
global Car newBranded(CarBrand brand)
{
Car car =
$[
"speed":0,
"brand":brand,
"colour":"red"
];
return car;
}
global void setSpeed(Car & instance, integer speed)
{
instance["speed"] = speed;
}
global integer getSpeed(Car & instance)
{
return instance["speed"]:0;
}
global CarBrand getBrand(Car & instance)
{
return (CarBrand)instance["brand"]:nil;
}
}
// include/Car.ycp
{
import "Car";
typedef map