| CARVIEW |
Select Language
HTTP/2 200
date: Wed, 31 Dec 2025 11:10:23 GMT
content-type: text/html
content-length: 8762
set-cookie: AWSALB=kLFOh6XWSwRos7SwKmAecCF0izx81g2wzk+zl0pQJNvbYly4lS6Q7onYATudX7KSa4Q5AHK0pI1CRdpPMGdBqlmrbHx95HJncGTpxnj77pY+UflluIrlTC2Xg2Bf; Expires=Wed, 07 Jan 2026 11:10:23 GMT; Path=/
set-cookie: AWSALBCORS=kLFOh6XWSwRos7SwKmAecCF0izx81g2wzk+zl0pQJNvbYly4lS6Q7onYATudX7KSa4Q5AHK0pI1CRdpPMGdBqlmrbHx95HJncGTpxnj77pY+UflluIrlTC2Xg2Bf; Expires=Wed, 07 Jan 2026 11:10:23 GMT; Path=/; SameSite=None; Secure
vary: Accept-Encoding
content-encoding: gzip
strict-transport-security: max-age=2419200
cache-control: max-age=60
x-powered-by: HHVM/4.164.0
Readonly: Introduction
How is
Getting Started
Source Code Fundamentals
Expressions And Operators
Introduction
Operator Precedence
Echo
Exit
Invariant
List
New
Subscript
Member Selection
Scope Resolution
Nameof
Incrementing And Decrementing
Error Control
Casting
Await
Type Assertions
Arithmetic
String Concatenation
Bitwise Operators
Logical Operators
Comparisons
Equality
Ternary
Coalesce
Pipe
Assignment
Yield
XHP Attribute Selection
Statements
Functions
Classes
Traits And Interfaces
Arrays And Collections
Types
Built In Types
Generics
Contexts And Capabilities
Reified Generics
Asynchronous Operations
Readonly
Modules
Packages
Attributes
XHP
Silencing Errors
Contributing
Experimental Features
Expression Trees
Memoization Options
Readonly: Introduction
readonly is a keyword used to create immutable references to Objects and their properties.
How does it work?
Expressions in Hack can be annotated with the readonly keyword. When an object or reference is readonly, there are two main constraints on it:
- Readonlyness: Object properties cannot be modified (i.e. mutated).
- Deepness: All nested properties of a readonly value are readonly.
Readonlyness
Object properties of readonly values cannot be modified (i.e. mutated).
class Bar {
public function __construct(
public Foo $foo,
){}
}
class Foo {
public function __construct(
public int $prop,
) {}
}
function test(readonly Foo $x) : void {
$x->prop = 4; // error, $x is readonly, its properties cannot be modified
}
Deepness
All nested properties of readonly objects are readonly.
function test(readonly Bar $x) : void {
$foo = $x->foo;
$foo->prop = 3; // error, $foo is readonly
}
How is readonly different from contexts and capabilities that control property mutation (such as write_props)?
Contexts such as write_props affect an entire function (and all of its callees), whereas readonly affects specific values / expressions.
Topics covered in this section
- Syntax: Basic syntax for readonly keyword
- Subtyping: Rules and semantics for interacting with readonly and mutable values
- Explicit Readonly Keywords: Positions where the readonly keyword is explicitly required
- Containers and Collections: Interactions between collections of readonly values
- Advanced Features and Semantics: More complex features and interactions
Thank You!
Thank You! If you'd like to share more feedback, please file an issue.