Home » Color Functions » Understanding Shadowing in Pine Script

Understanding Shadowing in Pine Script

Photo of author
Published on

Concept of Shadowing

Shadowing in programming refers to the scenario where a local identifier (like a variable or type name) has the same name as a broader scoped identifier, and thus, the local identifier “shadows” or overrides the broader one within its scope.

In Pine Script™, this concept is particularly relevant when considering the future-proofing of scripts against potential namespace conflicts.

UDTs and Object Names Shadowing Pine Script Namespaces

  • UDTs and Object Names: In Pine Script, user-defined types (UDTs) and object names are allowed to shadow the language’s namespaces. This means you can use names for your UDTs and objects that might coincide with built-in types or future additions to Pine Script.
  • Example: It’s permissible to name a UDT or object line or table, which are built-in types in Pine Script.

Restrictions on Primitive Types

  • Primitive Types: The language’s five primitive types (int, float, string, bool, and color) cannot be used as names for UDTs or objects. These primitive types are integral to the language’s structure and functionality, thus maintaining their distinct identity is crucial to avoid confusion and errors in script execution.

Practical Implications

  • Reduced Conflict Risk: By allowing UDTs and objects to shadow existing and future Pine Script namespaces, scripts are less likely to encounter issues when the language is updated or expanded.
  • Naming Flexibility: Script writers have more flexibility in naming their UDTs and objects, which can aid in creating more readable and organized code.

Conclusion and Key Takeaways

  • Shadowing in Pine Script allows UDTs and objects to use names that might coincide with built-in types or future language additions.
  • There is an exception for the language’s five primitive types, which cannot be shadowed.
  • This feature helps mitigate potential future conflicts and offers greater naming flexibility to script developers.

Understanding these nuances of shadowing in Pine Script is crucial for creating robust and future-proof scripts. In the next part, we will continue to explore advanced concepts and best practices in Pine Script programming. Stay tuned for more insights.

Leave a Comment