modular / modular/modular

[Feature Request] [mojo-lang] Reflection capabilities for functions in parameter space

Open
#6,529 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug_feature_triaged enhancement mojo Team: Mojo Compiler
Dominant language
Mojo
Stars
29.8k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

Review Mojo's priorities
What is your request?

I don't know if this is viable to implement in the compiler, but I thought I'd write this up in case it can.

# a trait that all functions conform to
trait AnyDef: ...

def do_something[func: AnyDef]():
    comptime for type in parameter_types(func): ...
    comptime for type in argument_types(func): ...
    comptime ret_type = return_type(func)
    ...

Bonus, if we could even treat effects as traits we could maybe build generic code around things like

def do_something[func: AnyDef]():
    comptime if conforms_to(func, thin): ...
What is your motivation for this change?
  • modular/modular#6521, part of the problem is that we don't have a way to know when a function's arguments or return type can be trivially converted from cpython directly to Mojo and do a roundtrip through the Python representation
  • The potential for this to change how we do generic functional code
Any other details?

I don't have enough examples off the top of by head, but this feels like a feature that will prove very powerful in several applications

I'll add @owenhilyard and @soraros in case they have some input

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Mojo roadmap and the existing compiler support for parameter-space functions, traits, effects, and compile-time introspection; no files or tests are named in the request. Define the supported reflection model and identify the relevant compiler entry points and tests before implementation, with completion requiring agreed behavior for parameter types, argument types, return types, and effects.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.