Prime Checker - Hack Typing CST Test
Loading…
Prime Checker — Hack Code
Checks if a number is prime.
<?hh
function isPrime(int $n): bool {
if ($n < 2) return false;
for ($i = 2; $i < $n; $i++) {
if ($n % $i === 0) return false;
}
return true;
}
<<__EntryPoint>>
function main(): void {
echo isPrime(13) ? "Prime\n" : "Not Prime\n";
}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.
Quick Explain
- ▸Hack combines dynamic typing of PHP with optional static typing.
- ▸It allows gradual typing, meaning developers can mix typed and untyped code.
- ▸Designed for web development at scale, particularly within the Facebook ecosystem.
Core Features
- ▸Gradual typing system
- ▸Async programming with awaitables
- ▸Generics and type annotations
- ▸Shape types for structured data
- ▸Hack collections (vec, dict, keyset)
Learning Path
- ▸Learn PHP basics if not already familiar
- ▸Understand Hack's gradual typing
- ▸Practice collections, shapes, and generics
- ▸Explore async programming with awaitables
- ▸Build sample web applications using HHVM
Practical Examples
- ▸Building a social media backend
- ▸Creating type-safe REST APIs
- ▸Async data processing pipelines
- ▸Integrating Hack with existing PHP code
- ▸High-performance web services
Comparisons
- ▸Adds static typing to PHP
- ▸Runs on HHVM instead of standard PHP engine
- ▸Better for large-scale web applications
- ▸Supports async and modern language features
- ▸Smaller ecosystem than mainstream PHP
Strengths
- ▸Improves reliability with static typing
- ▸Maintains compatibility with existing PHP code
- ▸Enhances code readability and maintainability
- ▸Optimized for large-scale web applications
- ▸Supports modern programming patterns
Limitations
- ▸Primarily used within Facebook or HHVM ecosystems
- ▸Smaller community compared to mainstream PHP
- ▸Requires HHVM runtime, not standard PHP engine
- ▸Not ideal for small-scale or non-web projects
- ▸Learning curve for developers new to static typing
When NOT to Use
- ▸Small-scale PHP projects
- ▸Environments without HHVM support
- ▸Non-web applications
- ▸Projects requiring extensive PHP library support
- ▸Legacy PHP hosting without Hack compatibility
Cheat Sheet
- ▸function foo(int $x): string { } - typed function
- ▸$var = vec[1,2,3]; - Hack vector
- ▸await asyncFunction(); - async call
- ▸shape('name' => string, 'age' => int) - structured data
- ▸hh_client - typecheck project
FAQ
- ▸Is Hack still used?
- ▸Yes, primarily in Facebook and HHVM-based projects.
- ▸Can Hack run on standard PHP?
- ▸No, it requires HHVM runtime.
- ▸Why learn Hack today?
- ▸For type-safe, high-performance PHP web development.
- ▸Is Hack open-source?
- ▸Yes, available under MIT license.
30-Day Skill Plan
- ▸Week 1: Hack syntax and typed variables
- ▸Week 2: Functions, collections, and shapes
- ▸Week 3: Async programming and awaitables
- ▸Week 4: Integrate Hack with PHP codebases
- ▸Week 5: Build and deploy small web services
Final Summary
- ▸Hack is a modern dialect of PHP with static typing and async support.
- ▸Runs on HHVM and enables type-safe, high-performance web applications.
- ▸Compatible with PHP codebases while adding modern language features.
- ▸Ideal for large-scale, maintainable web development.
Project Structure
- ▸src/ - main Hack codebase
- ▸lib/ - reusable libraries
- ▸tests/ - unit and integration tests
- ▸docs/ - project documentation
- ▸configs/ - HHVM and server configurations
Monetization
- ▸High-performance web apps
- ▸Backend services for large-scale systems
- ▸Enterprise PHP modernization projects
- ▸Consulting on Hack adoption
- ▸Training for Hack developers
Productivity Tips
- ▸Gradually add type annotations
- ▸Use async/await for concurrency
- ▸Modularize reusable logic
- ▸Test often with typechecker
- ▸Leverage Hack collections and shapes
Basic Concepts
- ▸Gradual typing (dynamic + static)
- ▸Collections (vec, dict, keyset)
- ▸Async programming with awaitables
- ▸Functions, classes, and traits
- ▸Shapes and generics for structured data
Official Docs
- ▸Hack Official Documentation
- ▸HHVM Documentation
- ▸Hack GitHub Repository