Access Rules
Extensions are located outside the original type definition and must comply with access control rules. Extensions can only access public and protected members of the extended type; they cannot access private fields and methods.
The Rectangle struct in the code on the right has a private _tag field and public width, height fields. Functions defined in the extension like area(), perimeter(), isSquare() can only calculate through public fields and cannot access _tag.
Extensions cannot shadow existing members of the extended type, nor can they shadow non-private members defined in other extensions. This ensures deterministic type behavior.
Note
Extensions themselves cannot use access modifiers, but extension members can use modifiers like
public, private, protected.Try accessing private fields in the extension and observe the compiler's error messages.