digitalmars.dip.ideas - if __ctfe and __ctfe functions
- Quirin Schroll (25/25) Jul 29 This was suggested before, but I’ll try to show another angle of
- Adam D. Ruppe (10/19) Jul 29 Note that this is implemented in OpenD, it works fine and enables
- Atila Neves (7/10) Jul 31 I still think using a contract and treating it specially is the
This was suggested before, but I’ll try to show another angle of this. It would be a new syntactic construct different from `if (__ctfe)`. We could also decide to special-case `if (__ctfe)`, either way: The branch in which `__ctfe` is guaranteed to be true should be ignored w.r.t. some attribute checks: In a `__ctfe` section, - a ` nogc` function can allocate, and - a ` safe` function can do pointer arithmetic. - *maybe:* a `nothrow` function can throw, except when control-flow reaches the `throw` expression, it halts for error. Allocation at CTFE isn’t what ` nogc` is about and safety checks are done at CTFE anyways even for runtime-unsafe operations. - Add `__ctfe` as a function attribute. - Overloading on `__ctfe` is possible and useful: Overload resolution for run-time code generation discards all `__ctfe` functions; overload resolution at CTFE prefers an overload if, after partial ordering, it is the only overload annotated `__ctfe` among the best matches. - A `__ctfe` function admits no safety attributes or ` nogc` (make no sense). - A `__ctfe` function is guaranteed not to be emitted into the binary.
Jul 29
On Tuesday, 29 July 2025 at 12:00:02 UTC, Quirin Schroll wrote:We could also decide to special-case `if (__ctfe)`, either way: The branch in which `__ctfe` is guaranteed to be true should be ignored w.r.t. some attribute checks: In a `__ctfe` section, - a ` nogc` function can allocate, andNote that this is implemented in OpenD, it works fine and enables some interesting pattern when combined with a default parameter bug fix. See: https://dpldocs.info/this-week-in-d/Blog.Posted_2024_11_25.html#to-set-the-stage-for-new-memory-patterns- Overloading on `__ctfe` is possible and useful: Overload resolution for run-time code generation discards all `__ctfe` functionsSame result can be achieved by the if(__ctfe) {} branch....- A `__ctfe` function is guaranteed not to be emitted into the binary.this is something opend tried to do via a pragma, but had to revert since our impl introduced regressions. some day we'll get back and finish it though.
Jul 29
On Tuesday, 29 July 2025 at 12:00:02 UTC, Quirin Schroll wrote:This was suggested before, but I’ll try to show another angle of this. [...]I still think using a contract and treating it specially is the way to go since all we have to do is change the implementation: string myStringMixin() in(__ctfe) { }
Jul 31