Skip to content

Conversation

@Pieter12345
Copy link
Contributor

@Pieter12345 Pieter12345 commented Oct 12, 2025

Add __cast__() function with (type) value syntax.

The main purpose of this PR is to share code and serve as a basis for MethodScript design discussions.

In the current implementation, __cast__() only performs regular non-conversion casts (i.e. no cross-casts / data conversions). An idea is to use (type) value syntax for the non-conversion casts and value as type syntax for cross-casts (including non-conversion casts).

TODOs:

  • Remove (common) double runtime checks. This is mainly about optimizing assign(type, __cast__(val, type)). to assign(type, val). An interesting alternative is to remove the type check from assign() and instead wrap its value in a __cast__(), which allows for removal of the entire runtime type check if this has been validated in compile-time already. That implementation is not trivial though.
    EDIT: Implemented by rewriting assign() to __unsafe_assign__() when its typecheck is known to pass during optimization.
  • Remove nested __cast__() functions that cast to the same type. Also remove nested casts where there is a stronger check followed by a weaker check (the weaker check will always pass if the stronger check will pass).
  • Add compile warning on casting to the exact type that the value already has.
  • Add compile error on casts that can never pass.
  • Remove __cast__() if the value is instanceof the type being cast to (always passing casts). This will be easy for code that optimizes to constants and very hard to impossible for code that is variable (and might get its type from a different file).
    EDIT: This is implemented for static values (that are optimized in compile time). It likely won't be implemented for dynamic values based on their type information.
  • assign() optimization that no longer clones ivariables causes pass-by-value variables above a closure to be changed by modifications within the closure. Either a new IVariable instance needs to be created or environment cloning needs to create this copy to avoid this issue.

@LadyCailin LadyCailin requested a review from Copilot October 13, 2025 15:23
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a __cast__() function to support type casting with (type) value syntax in MethodScript. The implementation focuses on non-conversion casts and serves as a foundation for design discussions.

Key changes:

  • Adds __cast__() internal function for type casting operations
  • Implements compiler rewriting logic to convert (type) value syntax to __cast__(value, type) calls
  • Updates parenthesis handling to distinguish between casts and regular parenthetical expressions

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Pieter12345 Pieter12345 force-pushed the cast-function branch 3 times, most recently from 1a571de to ab92ab8 Compare October 19, 2025 22:21
Add `__cast__(val, type)` function and syntax. Syntax is `(type) val`.
- Mark `__cast__()` for constant and cached returns.
- Remove nested casts where the second executed cast is removed if the first executed cast passing ensures that the second executed cast will pass.
Optimize `assign()` to `__unsafe_assign__ ()` when it is known that the `assign()` typecheck will always pass.
- Do not redefine variables in variable list when not necessary.
- Unwrap IVariable values only once.
- Directly create new IVariable with correct values when necessary.
Fixes compile error in the following example code:
```
@A = (1 + 2)
msg(123)
```
Allows for not recreating a new `IVariable` for every assign operation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant