Language Idea

Old Alpha Concept

While programming languages evolved, concepts for function pointers and lambdas came up. In some way variables are simply functions returning a single value and functions returning an independent value are constant variables. Possibly this function would be optimized into a single constant in some languages.

int ten() {
	return 10;
}

Nowdays in some languages it is possible to declare a delegate and assign various lambda expressions to it. It is natural that the next step in this evolution is to merge these concepts into one type. These are the alphas.

int ten;

ten = 10;

ten {
	return 10;
}

int add(int a);

add(int a) = a + 1;

add(int a) {
	return a + 1;
}

add(int a) {
	return a + ten;
}

Look at the code below. The c variable gets independent. Its value is based only on the assign-time a and b values. The d function depends on the access-time values but cannot be overassigned. These are limitations we want to solve while still keeping the original behaviuor with a nice syntax. Every variable or function call can have a sign before it which shows wether we want it to be evaluated assign-time or access-time. Let us call these cases late and instant evaluation.

int a = 10;
int b = 20;

int c = a + b;

int d {
	return a + b;
}

a = 20;
b = 30;

// c is 30
// d is 50

int c = @ a + @ b;

int d {
	return $ a + $ b;
}

a = 10;
b = 20;

// c is 30
// d is 50

How this could be implemented? My suggestion is to compile everything as function pointers. This might look as speed reduction, but in the proper cases they should be optimized out by the compiler to simply be normal variables or functions.

int two = one;
// value copied

int three = @ one;
// function pointer created

int z(int a, int b) = x(a, b);
// original function pointed

int v(int a, int b) = @ x(a, b);
// function pointer created

When accessing an alpha, it gets reduced or evaluated. This means that it will be called or the refered alpha will be evaluated recursively. When assigning an alpha with the instant evaluation syntax to another we expect it not to depent from any other one and this holds.

int a = 10;

int b = @ a;

int c = b;

// b is 10
// c is 10

a = 20;

// b is 20
// c is 10

int d(int x) {

    a = 30;

    // parameters also work
    // callin with b would
    // result x to be 20
}

An important note is that alphas with zero parameters are not the same as alphas without parameters. Logically they are, but this separation can used to add hint for the programmer wether the alpha does some heavy calculation or a simple and quick operation. This example also shows some syntax-candy usage.

class Point {

	private double x;

	private double y;

	public const double sum = @ x + @ y;

	public const double hash {
		return hash(sum);
	}

	public normal() {
		return sqrt(sq(x) + sq(y));
	}
}

Hard and Weak

Hard References work similar to C-plus-plus references. They can be only be constructed from a source alpha or reference. From that point every assignment and member acces will be redirected to the source alpha.

Note that in both the fallowing examples the developer will spare copying huge amount of data but is only able to use the argument as outgoing in the second case. This snippet also shows that one can call any function without copying, while being sure that the argument will not be overassigned. Be warned to used unknown functions this way, since they may change members.

HeavyStruct one;
HeavyStruct two;

void assignOne(HeavyStruct a, HeavyStruct b) {
	b = a;
}

assignOne(@ one, @ two);

// weak referenced
// two not changed

void assignTwo(HeavyStruct hard a, HeavyStruct hard b) {
	b = a;
}

doSomethingTwo(one, two);

// hard referenced
// two is changed

Waek References work similar to what we are used to in D reference types and C-sharp reference types. They can be constructed based on a source alpha or a hard reference or another weak reference. The values do not get copied and assignments to the weak referece are not redirected only member accesses. They point to the data so when the source gets reassigned then the weak reference will still point to the original entity.

struct Integer {
	int x;
}

Integer a(1);

Integer weak b = a;
Integer weak c = a;

a.x = 2;

// b.x returns two
// c.x returns two

a = Integer(3);

// b.x is still two
// c.x is still two

b.x = 1;

// b.x is now one
// c.x is now one

Pure alphas are consts with the additional compile-time criteria that they can only depend from their arguments and other pures. This can ensure that even their reduced value will never changed. So theoretically they should be calculatable at compile time for known arguments.