Skip to content

[refactoring] shared Undefined and Void type instances #255

Open

Description

@Stepami

Specification: shared Undefined and Void type instances

Status: Proposed; implementation is not part of this document.
Date: 2026-09-29.

Objective

Reduce repeated allocations in static analysis and operator result handling by reusing the two fixed type values undefined and void. Rename the IR base type to HydraType and expose the shared values on that class.

The existing Type conversion constructs a new instance for each string conversion. Construction also creates the per-instance operator collection and registers equality operators. NumberType.Instance provides the existing public static readonly field convention.

The source inventory contains 19 conversion sites under src: 14 fixed-value sites covered by this change and five dynamic-name sites that continue to require their own values. Counts describe source locations, not runtime allocation counts.

1. Rename Type to HydraType

Rename HydraScript.Domain.IR.Types.Type to HydraScript.Domain.IR.Types.HydraType and rename Types/Type.cs to Types/HydraType.cs. Keep the namespace and project boundaries.

Use Rider's semantic rename to update references throughout the solution, including production code, tests, and benchmarks. Update base classes, generic arguments, signatures, casts, equality members, the conversion operator, and aliases that refer to this symbol. Remove obsolete Type aliases and use HydraType consistently in these type references.

Members named Type (such as ISymbol.Type and ArrayType.Type), System.Type, and separate symbols such as TypeValue, TypeSymbol, and NumberType retain their names. Update documentation references and file links affected by the rename, including ADRs 0005 and 0006.

This is a public CLR API rename and requires consumers to use the new name. No compatibility class with the old name is introduced. HydraScript source-language syntax, type spellings, and diagnostics retain their current behavior.

2. Add shared fields

Declare these members on HydraType:

public static readonly HydraType Undefined = "undefined";
public static readonly HydraType Void = "void";

Both initializers must use the implicit string conversion. They are fields, following NumberType.Instance, rather than allocating getters. Their runtime type is the base HydraType; no UndefinedType or VoidType subclasses are needed.

Retain the conversion implementation after the rename:

public static implicit operator HydraType(string alias) =>
    new(alias);

Do not intern strings or route this operator through Undefined or Void. Arbitrary-name conversion remains available, and the field initializers must not depend recursively on the fields being initialized.

The base instances can be shared with the current implementation: the name is readonly, the private operator dictionary is populated during construction and only queried afterward, and base ResolveReference is a no-op. Preserve those properties for the shared instances.

3. Replace fixed-value conversion sites

Replace the following expressions with HydraType.Undefined or HydraType.Void. Preserve the surrounding control flow, success flags, and operator checks.

Paths in the table are relative to src. Line numbers identify the pre-change inventory and will move during implementation.

File Existing value and line Replacement
Application/HydraScript.Application.StaticAnalysis/Impl/HydraScriptTypesService.cs "undefined", 37 HydraType.Undefined
Application/HydraScript.Application.StaticAnalysis/Impl/HydraScriptTypesService.cs "void", 39 HydraType.Void
Domain/HydraScript.Domain.IR/Types/Operators/ArithmeticOperator.cs "undefined", 23 HydraType.Undefined
Domain/HydraScript.Domain.IR/Types/Operators/ArrayConcatOperator.cs "undefined", 11 and 17 HydraType.Undefined
Domain/HydraScript.Domain.IR/Types/Operators/ComparisonOperator.cs "undefined", 9 HydraType.Undefined
Domain/HydraScript.Domain.IR/Types/Operators/ConditionalOperator.cs "undefined", 23 HydraType.Undefined
Domain/HydraScript.Domain.IR/Types/Operators/EqualityOperator.cs "undefined", 9 HydraType.Undefined
Domain/HydraScript.Domain.IR/Types/Operators/IndexOperator.cs "undefined", 9 and 21 HydraType.Undefined
Domain/HydraScript.Domain.IR/Types/Operators/LengthOperator.cs "undefined", 9 HydraType.Undefined
Domain/HydraScript.Domain.IR/Types/Operators/RemoveFromArrayOperator.cs "undefined", 9 HydraType.Undefined
Domain/HydraScript.Domain.IR/Types/Operators/RemoveFromArrayOperator.cs "void", 14 HydraType.Void
Domain/HydraScript.Domain.IR/Types/Operators/StringConcatOperator.cs "undefined", 9 HydraType.Undefined

Keep the built-in type service API, with its return types updated by the rename:

public HydraType Undefined => HydraType.Undefined;
public HydraType Void => HydraType.Void;

Existing callers of these properties automatically receive the shared instances. Keep built-in registration and dependency injection behavior intact. Operator implementations reference the fields directly within Domain.IR.

Dynamic names and scope boundaries

These five conversion sites retain their existing logic, with symbol references updated by the rename:

File relative to src Expression and line Reason
Application/HydraScript.Application.StaticAnalysis/Visitors/TypeSystemLoader.cs visitable.TypeId.Name, 45 and 50 Checks and registers the actual declared type name.
Domain/HydraScript.Domain.IR/Types/ArrayType.cs refId, 22 Compares an unresolved array element type with the reference name.
Domain/HydraScript.Domain.IR/Types/NullableType.cs refId, 14 Compares an unresolved nullable type with the reference name.
Domain/HydraScript.Domain.IR/Types/ObjectType.cs refId, 67 Compares an unresolved property type with the reference name.

Neither shared field represents an arbitrary user-defined name. Optimizing these dynamic conversions would require a separate design.

Direct constructor calls, other built-in singleton implementations, AST string values, and language string comparisons are outside the allocation replacement scope. Preserve structural and directional equality, hash codes, string representations, operator registration, and reference resolution. Shared instances must not lead to replacing semantic equality with reference equality.

Acceptance criteria

  1. The solution uses HydraType for the renamed IR class, and all affected source references and documentation links resolve.
  2. HydraType.Undefined and HydraType.Void are public static readonly fields initialized by the exact string literals above. They represent distinct instances with string representations undefined and void.
  3. Repeated reads of either type-service property, including reads from different service instances, return the corresponding field instance.
  4. All 14 fixed-value conversion sites use the fields. Under src, the only remaining conversions of those two literal values into HydraType are the field initializers.
  5. Operator branches formerly producing these values return the corresponding shared instance. Existing success/failure outcomes and all other result types remain unchanged, including ArrayConcatOperator's fallback behavior.
  6. Shared values retain name-based equality, matching hash codes, and equality-operator support. String conversion of arbitrary names still creates a correctly named instance, and dynamic declaration/reference resolution continues to work.
  7. Once static initialization completes, evaluating a replaced expression constructs no new HydraType or per-instance operator collection. The two field initializers still incur construction costs once per type initialization. This does not promise that the surrounding analysis or operator call is allocation-free.

Implementation validation

Follow repository testing guidance and the required Rider skills during implementation. Locate existing tests with finding-tests, use code-testing-agent for focused additions, and load run-tests before choosing or executing test commands.

Use focused reference-identity regressions for the shared values, service getters, and affected operator result paths. Pair them with the existing semantic checks for equality, operators, and declaration/reference resolution. Reference-identity checks establish reuse; allocation counters or timing assertions are not required in unit tests.

Inspect Rider diagnostics and build the full solution after the rename. Run focused regressions followed by the unit and integration suites because the renamed public base type crosses the IR and static-analysis stages. Verify nonzero executed test counts and the applicable integration coverage gate. Validate changed documentation links and formatting.

No numerical performance target is claimed by this spec. If reporting measured savings, compare the same controlled workload before and after the change using the existing benchmark guidance, with static initialization accounted for separately.

Documentation handling

This document describes proposed work. Keep current-implementation guidance and ADRs unchanged until implementation lands; then repair renamed links and descriptions. This refactor fits the existing allocation guidance and structural type-system decision, so it does not require a new architectural pillar ADR.

Activity

  1. added
    IRWorking on intermediate representation of the interpreter
    on Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    IRWorking on intermediate representation of the interpreterImprovement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions