Skip to content

Instantly share code, notes, and snippets.

@secretGeek
Last active July 27, 2019 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save secretGeek/317994b4b9190287c3c6beff1ecb2ff4 to your computer and use it in GitHub Desktop.
Save secretGeek/317994b4b9190287c3c6beff1ecb2ff4 to your computer and use it in GitHub Desktop.
example of language with self-contained functions

Xah Lee said: https://twitter.com/xah_lee/status/1154957785441816576

i really want a language where you can copy paste any function definition in any project and use in another project. doesn't seem to exist. For this to work, the function param must encode any dependency, of global var and libs.

It took me a while to get my head around this idea, but here's some thoughts so far.

(I'll use C# as my example syntax here)

Imagine a function that is already self-contained....

double AreaOfASquare(double side) 
{
	    return side*side;
}

Okay, that's easy - you can copy paste that and it will "just work".

But what if you wrote this function...

double AreaOfACircle(double radius) 
{
    return radius * radius * Math.PI;	
}

Hypothetically, someone might copy paste that somewhere else, and let's imagine that the target recipient of the function might fail with an error like

"Unknown Class: "Math" Are you missing a using statement or class definition?

There might be some extension to the language where the function's dependencies can be defined in a self-contained way, such as:

double AreaOfACircle(double radius) with Math=System.Math(version=1.3)
{
    return radius * radius * Math.PI;	
}

Maybe the tooling might allow you to right click a function name and choose "copy function as self-contained function" -- which would generate the "With" statement.

What if the function depended on other packages, including local functions... then it gets a bit trickier and we're heading into "mini-packages" in the style of npm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment