Category:Sparkling: Difference between revisions

Line 31:
<lang sparkling>const helloWorldString = "Hello, world!";
 
function printMessage(msg) {
{
var numCharsWritten = printf("%s\n", msg);
printf("%d characters written\n", numCharsWritten);
Line 49 ⟶ 48:
$
</pre>
 
=== Fundamental language features ===
 
==== Types ====
Sparkling is a dynamically but strictly typed language. Typically, binary operators operate on values of the same type only, which is enforced by runtime checks. Standard library functions type-check their arguments as well. There are 7 types in the language:
* Nil ("nil"): the empty value. It has only one possible value: <tt>nil</tt>. It indicates the absence of any other meaningful value. It is the result of array indexing by a key that is not found, the return value of functions that don't return anything explicitly, the default initial value of local variables, and the value of missing function arguments.
* Boolean ("bool"): Sparkling has a proper Boolean (true/false) type. Conditions of the if statement and loops and logical operators require their condition to evaluate to a Boolean value (otherwise, a runtime error is raised -- there are no implicit conversions to Booleans).
* Number ("number"): either a signed integer (C's <tt>long</tt> type) or a double-precision floating-point number. The only type of implicit conversion in the language is between integers and floating-point numbers (if an arithmetic operator has an integer and a floating-point operand, it will treat both values as floating-point and it will produce a floating-point result as well.)
* String ("string"): a sequence of bytes, which is not necessarily a human-readable string (it can contain arbitrary binary data). When exposed through the C API, a terminating 0 byte is always appended to the end of string. It does not count against the length of the string object, but it makes standard C functions to operate on Sparkling strings easily.
* Array ("array"): a fast associative array. The array is the only mutable data structure in the language. The type of its keys and values can be of any type, but indexing by the floating-point <tt>NaN</tt> value is not permitted. Sparkling's array implementation is a hybrid data structure: in general, all keys are stored in a hash table, but sufficiently small integer keys are stored implicitly in a vector, so if an array is being used as a non-associative, indexed array, then it has very good performance.
* Function ("function"): in Sparkling, functions are first-class values. They can be passed to functions as arguments and returned by functions. There is a literal syntax for creating - possibly unnamed - function objects. Sparkling has lexical closures, so functions capture variables (by value) from outer scopes. All functions are variadic; it is not an error to call a function with more or less arguments than the number of arguments it is defined with.
* User information objects ("userinfo"): in order to facilitate the creation of third-party extension libraries, Sparkling has a "joker" data type, the user info. User info objects have two subtypes: weak and strong. Weak user info values hold an arbitrary, unmanaged, generic C pointer (<tt>void *</tt>), while strong user info objects contain a managed, reference-counted Sparkling object of an user-defined "class". (The Sparkling API makes it possible for users to define their own classes -- memory-intensive built-in objects like strings and arrays already use this mechanism for managing memory.)
 
Being a rudimentary feature of reflection, Sparkling supports querying the type of an object at runtime, using the <tt>typeof</tt> operator.
 
==== Variables ====
 
==== Expressions ====
 
==== Statements ====
 
 
==== Functions ====
 
A full language tutorial is available in the official documentation[https://github.com/H2CO3/Sparkling/blob/master/doc/tutorial.md].
Anonymous user