mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[Feature Request] Add functionality like `@classmethod` to enforce conditional conformance on struct static methods

Open martinvuyk opened this issue 7 months ago • 0 comments

Review Mojo's priorities

What is your request?

Add functionality like @classmethod to enforce conditional conformance on struct static methods

What is your motivation for this change?

Being able to constraint the calling of a static method according to struct parameters

    @classmethod
    fn create_server(
        cls: Socket[SockFamily.AF_INET, SockType.SOCK_STREAM, *_],
        address: IPv4Addr,
        *,
        backlog: Optional[Int] = None,
        reuse_port: Bool = False,
    ) raises -> __type_of(cls):
        ...

Any other details?

I don't think it would be a good idea to call them @classmethod like Python because it would tie the implementations together (I have no idea what the dynamism of Classes will entail but I imagine it won't be a good idea to mix them together) and be confusing conceptually like @gryznar pointed out in issue #2010 , but I wouldn't remove the method part either. I think just calling them @structmethod and making the first arg stc (convention, but can call them whatever) will be easy to explain to python devs.

martinvuyk avatar Jul 25 '24 01:07 martinvuyk