The main rule is:
Пиши как сигма - это главное, остальное не так важно.
- Use camel_case to name functions and variables.
- Use flatcase or kebab-case to name components.
- Use flatcase to name libraries.
Here's how you should organize your scripts:
lib
directiveuse
directives- Functions
- Components
- Variables
- Executable code
You should split big scripts into smaller ones with similar functions/components/variables.
Prefix internal functions/components/variables with an underscore (_). Prefix exported names with library name and an underscore:
lib example;
_internal_var = 1;
fn _internal_fn() {
ret "internal lib data";
}
example_var = 2;
fn example_fn() {
ret example_var + _internal_var;
}
Prefixing exported names is not necessary, but this makes it easier to understand which libraries functions belong to. Also, if you prefix exported names it is impossible for functions/variables/components from different libraries to overlap.
SigmaScript is very strict when it comes to scopes.
For example, you can not change variables in parent scope from function scope:
x = 0;
fn test() {
x = 1;
}
test();
print x;
x
will be still zero, even though the function test
changes its value. But test
only changes the x
value in local function scope, so these changed values will be only accessible from inside the function.
If you want to mutate data from different scope (e.g. global variables, library variables) you should use references:
use ref;
x = ref(0);
fn test() {
ref_set(x, 1);
}
test();
print ref_get(x);
In SigmaScript, functions, components and variables are different things. They are stored in different places, so you can have components, functions and variables with the same name without any conflicts. This behaviour makes it impossible to pass a function as an argument. The reason is simple: everything in SS can be represented using JS strings. But you can't put function in a string. So, functions are stored in different namespace. This makes clear distinctions between functions and variables.
So instead, you could store reference to a function in a variable and then pass it as a callback. You can do this in two ways: using fn
lib and using lambda functions.
Instead of using the fn
method, you should use a more clear approach: lambda functions. Lambda functions are expressions that can be stored in variables and passed as arguments. They can be defined using =>
syntax:
lambda = () => {
print "Hello world!";
};
To call a lambda function simply use call
:
use fn;
// ...
call(lambda);
- Don't try to change value of variables from parent scope. Use refs instead.
- Avoid using
js
library when possible. Instead, use a native library (or write your own). It is hard to read and it runs slower than regular JS code. - Comment your code, but don't write too much comments.
- Use SSX instead of
dom
. - Use lambda functions instead of
fn
. - Star this repository!