Hello. I am Timur. I am machine (hydraulic presses) repairer with some programming skils (basic c++ for the moment).
Does Pascal support higher-order functions? Let’s say I want to change the behavior of a character. in c++, I can pass either a pointer to a function or a lambda or std::function inside a class and thus I pass a new behavior program. I do not know if there is a similar thing in the Pascal language.
There is example on cpp (the one language I more-less know for the moment, but pascal looks interesting).
#include
#include
#include
struct fine_grinder
{
void run(std::string coffee_beans)
{
std::cout << "cool! fine grinder works with " << coffee_beans << std::endl;
}
};
struct bad_grinder
{
void run(std::string coffee_beans)
{
std::cout << "baaad grinder works with " << coffee_beans << std::endl;
}
};
// templated function that will use any grinder
template void grinder_func(std::string coffee_beans)
{
Grinder_type grinder;
grinder.run(coffee_beans);
}
struct Cofemachine
{
void grind_coffee(std::string coffee_beans)
{
grinder_(coffee_beans);
}
void set_grinder(std::reference_wrapper<void(std::string)> grinder)
{
grinder_ = grinder;
}
std::reference_wrapper<void(std::string)> grinder_ = grinder_func<bad_grinder>;
};
int main()
{
Cofemachine mycoffemaker;
mycoffemaker.grind_coffee(std::string{"coffee_beans coffee"});
// here we pass into mycoffemaker the grinder_function filled with fine_grinder object
mycoffemaker.set_grinder(grinder_func<fine_grinder>);
mycoffemaker.grind_coffee(std::string{"coffee_beans coffee"});
return EXIT_SUCCESS;
}