Counter and Theme Toggle - Hack Typing CST Test
Loading…
Counter and Theme Toggle — Hack Code
Demonstrates a simple counter with theme toggling using Hack variables, functions, and conditional statements.
<?hh
<<__EntryPoint>>
function main(): void {
$counter = 0;
$isDark = false;
function updateUI(): void {
global $counter, $isDark;
echo "Counter: $counter\n";
echo "Theme: " . ($isDark ? "Dark" : "Light") . "\n";
}
function increment(): void {
global $counter;
$counter += 1;
updateUI();
}
function decrement(): void {
global $counter;
$counter -= 1;
updateUI();
}
function reset(): void {
global $counter;
$counter = 0;
updateUI();
}
function toggleTheme(): void {
global $isDark;
$isDark = !$isDark;
updateUI();
}
// Simulate actions
updateUI();
increment();
increment();
toggleTheme();
decrement();
reset();
}Hack Language Guide
Hack is a programming language developed by Facebook as a dialect of PHP. It adds static typing, enhanced performance, and modern programming features while maintaining compatibility with PHP, enabling rapid development of large-scale web applications.
Primary Use Cases
- ▸Large-scale web application development
- ▸Maintaining and modernizing PHP codebases
- ▸Developing scalable backend services
- ▸Improving code safety with static typing
- ▸Rapid iteration and prototyping in web projects
Notable Features
- ▸Optional static typing for safety and performance
- ▸Type inference to reduce boilerplate
- ▸Compatibility with PHP codebases
- ▸Collections and async support
- ▸Generics and shape types for advanced type modeling
Origin & Creator
Developed by Facebook in 2014.
Industrial Note
Hack is mainly used for large-scale web applications, especially for projects requiring high performance, maintainability, and gradual typing within PHP-based systems.