Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow a package to forbid its users from overloading a function #54138

Open
nsajko opened this issue Apr 18, 2024 · 1 comment
Open

allow a package to forbid its users from overloading a function #54138

nsajko opened this issue Apr 18, 2024 · 1 comment
Labels
design Design of APIs or of the language itself feature Indicates new feature / enhancement requests

Comments

@nsajko
Copy link
Contributor

nsajko commented Apr 18, 2024

Motivation

Sometimes when designing an API, one wants to have a pair of public functions, where one is meant to be called by users, while the other is for users to overload (add methods to). An example in Base is the promote_type and promote_rule function pair. The promote_type doc string is clear about users being forbidden from overloading this function:

Don't overload this directly

To overload promotion for your own types you should overload promote_rule.

Still, many packages do overload promote_type:

https://juliahub.com/ui/Search?q=promote_type&type=symbols&u=define&t=function

Proposed solutions

Some alternatives:

  1. Allow a function to be marked as overloadable only from the module where it's declared
  2. Allow a function to be marked as overloadable only from the package where it's declared

Nota bene

This issue arguably ties into the function/method sealing issue: #31222.

This issue arguably ties into the API specification issue: #49973.

@vtjnash
Copy link
Member

vtjnash commented Apr 19, 2024

There is a hidden field of MethodTable that can be set to enable this (it exists for Builtin, as the runtime would likely segfault if someone unintentionally added a Method there). It is almost certainly not stable, but may be useful anyways to set inside Base, where overloading the wrong method may be common and lead to confusion (eg perhaps promote_type) or bugs (eg perhaps typejoin). However, it currently isn't general enough to prevent abusing getproperty to introduce unsoundness either, as it works at the MethodTable level, not on particular Methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of APIs or of the language itself feature Indicates new feature / enhancement requests
Projects
None yet
Development

No branches or pull requests

2 participants