8
$\begingroup$

Using PrintDefinitions give one a great view into many system functions. Often the design of these symbols incorporate multiple patterns that I see again and again. To name a few:

  • For a symbol XYZ, there is iXYZ[], aXYZ[], etc...
  • Use of so called “router” subroutines.
  • Specialized failure catch/throw mechanisms
  • Fancy nonstandard option handlings
  • Autoloading/Dumping contexts

The source doesn’t look at all like the normal functions people write in packages. So I was hoping a developer could respond with a little tutorial on any interesting patterns that WRI likes to use for some built-in functions.

$\endgroup$
  • 3
    $\begingroup$ I've never seen an aXYZ -- what's an example? I think of iXYZ as "internal XYZ." The parent XYZ[] does argument checking and possibly other preprocessing of arguments and options; then sends the prepared arguments to iXYZ[] for the main computation. If the main computation fails, the internal function often returns $Failed, which causes XYZ[] to return unevaluated. Something like this: XYZ[args___] := Module[{res}, (* check/process args *); res = iXYZ[args]; res /; FreeQ[res, $Failed]]; As noted, they sometimes use Catch and Throw for a nonlocal return of $Failed. $\endgroup$ – Michael E2 Jun 11 at 5:14
  • 3
    $\begingroup$ "Fancy nonstandard option handlings" explains why do many builtins have broken option handling ... in particular, why many won't pick up things set with SetOptions. $\endgroup$ – Szabolcs Jun 11 at 13:20
  • $\begingroup$ I really don't think there many techniques used there which are not in some answers scattered on this site. What you mentioned amounts to using SubValues, local variables shared between the Module / With and the condition, exceptions with custom tags to fail early, delegeation of implementation to dedicated separate internal functions. All of that has been discussed in some Q/As on this site. $\endgroup$ – Leonid Shifrin Jun 12 at 23:19
  • $\begingroup$ @LeonidShifrin That maybe true but the keyword is scattered. A unified answer combining these and more internal folklore would be quite helpful! $\endgroup$ – M.R. Jun 12 at 23:33
  • $\begingroup$ I will try to remember to return to this Q/A when I get a spare moment big enough to do it justice. Not sure when that will be though. Hopefully someone else would jump in in the mean time. $\endgroup$ – Leonid Shifrin Jun 12 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.