Out of curiosity, are there any languages that let you tag a function with an attribute that indicates that it may only be called from a single call-site, such that if any code tries to call the function from a second call site, the second call-site is flagged as a compile-time error?

For example (pseudocode):

unique_call_site void myFunction(int a, int b, int c) {...}


myFunction(1, 2, 3);  // ok
myFunction(1, 2, 3);  // error -- second call site not allowed!

Motivation for the question: it's sometimes desirable to break functionality out of a function and into its own separate sub-function (if only to keep the original function from getting too large and complicated), but once you do that, then anyone examining the sub-function now has to consider all the different calling-contexts that the sub-function might possibly be called from, and that consideration re-introduces a bit of complexity. Having the sub-function tagged with a keyword that indicates that it can be called from only a single context (and having the compiler enforce that property) would allow the reader to know right away that they only need to consider that single calling-context when studying how the sub-function might be used.

closed as off-topic by gnat, Doc Brown, Thomas Owens Mar 17 at 22:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking us to find or recommend tools, libraries, programming languages, resources (including books, blogs, tutorials, and examples), or projects to undertake are off-topic here as they attract opinionated answers that won't have lasting value to others. You may be able to get help in The Whiteboard, our chat room." – gnat, Thomas Owens
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Would you not be calling that function several times from test code? If its complex enough to break off, then its complex enough to warrant individually unit testing it. – Kain0_0 Mar 14 at 5:08
  • 3
    FWIW, C++ classes have a "friend" mechanism, and C# an "InternalsVisibleTo", which work both at the granularity of classes, not functions (but these can be utilized to accomplish what you are asking for). However, list-of-things questions are not welcome here . – Doc Brown Mar 14 at 5:21
  • Javascript Function.caller (non-standard) would let you get close to what you describe. It would be a run-time error, not compile time. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I don't think it's a practical answer and would NOT recommend using it. – joshp Mar 14 at 6:51
  • 1
    to avoid closing this question as beeing to broad: can you add a specific language so we can discuss here how this language handles this? – k3b Mar 14 at 9:55
  • 1
    Rust has this in the std::ops::FnOnce trait – Alexander yesterday

Not that I am aware of, but many languages allow something like this:

void SomeFunction() {
  void InnerFunction(int a, int b, int c) {
    // do stuff!

  // blah blah

// some other context
InnerFunction(1,2,3); // error!

Which allows you to limit the scope of your sub-functions to the single place they're used.

I personally don't care for this syntax since it hinders testability, adds complexity to the language (parser, name resolution, tooling to name a few), and doesn't provide a whole lot of readability benefit over a more general feature (like simple private functions). If you need to know how often something is used, something like CodeLens does that better without modifying the language. YMMV.

  • 1
    A different way to achieve that, also supported in many languages, is storing a lambda and calling it a bit later. – Deduplicator Mar 15 at 1:29

Many OO languages support private methods/functions, that are located within a class, and can only be called from within that class.
You can simply make a small class that conatins only your private method and the method that is allowed to call it.

  • This does not do what the question asks for. The 'unique' method could still be called multiple times by the containing small class. – Graham Mar 15 at 16:50
  • @Graham - if the class cannot be extended, then only if someone edits the class to do so. At which point they could also just delete whatever protection specifier was being used, make the private function public, etc... – Chris Stratton Mar 18 at 2:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.