### Example Struct Usage Source: https://github.com/gnome/vala/blob/main/doc/manual/source/structs.rst A placeholder for demonstrating struct functionality. This snippet is intended to be expanded with concrete examples. ```vala // ... ``` -------------------------------- ### Download and Compile Vala Release Tarball Source: https://github.com/gnome/vala/blob/main/README.md Use this command to download a Vala release tarball, extract it, configure the build with a specified installation prefix, and then compile and install it. This method is useful for bootstrapping or when a pre-existing valac is not available. ```sh curl --silent --show-error --location https://download.gnome.org/sources/vala/0.48/vala-0.48.25.tar.xz --output vala-bootstrap.tar.xz tar --extract --file vala-bootstrap.tar.xz cd vala-bootstrap ./configure --prefix=/opt/vala-bootstrap make && sudo make install ``` -------------------------------- ### Clean and Compile Vala from Repository Source: https://github.com/gnome/vala/blob/main/README.md Clean all untracked files from the Vala source tree using `git clean -dfx`, then run the autogen script, and compile and install. This is useful for starting a fresh build. ```sh git clean -dfx ./autogen.sh make && sudo make install ``` -------------------------------- ### Vala Interface Virtual Method Example Source: https://github.com/gnome/vala/blob/main/doc/manual/source/interfaces.rst Demonstrates an interface with a virtual default method and classes that either inherit or override this method. Shows how object instances use the most specific implementation. ```vala /* This example gives you a simple interface, Yelper, with - one virtual default method, yelp It shows you two classes to demonstrate how these and overriding them behaves: - Cat, implementing Yelper (inheriting yelp) - Fox, implementing Yelper (overriding yelp) Important notes: - generally an object uses the most specific class's implementation - Yelper provides a default yelp (), but Fox overrides it - Fox overriding yelp () means that even casting Fox to Yelper still gives you Fox.yelp () - as with the Speaker/speak () example, if a subclass wants to override an implementation (e.g. Fox.yelp ()) of a virtual interface method (e.g. Yelper.yelp ()), it must use 'new' - 'override' is used when overriding regular class virtual methods, but not when implementing interface virtual methods. */ interface Yelper : Object { /* yelp: virtual, if we want to be able to override it */ public virtual void yelp () { stdout.printf (" Yelper yelps Yelp!\n"); } } /* Cat: implements Yelper, inherits virtual yelp () */ class Cat : Object, Yelper { } /* Fox: implements Yelper, overrides virtual yelp () */ class Fox : Object, Yelper { public void yelp () { stdout.printf (" Fox yelps Ring-ding-ding-ding-dingeringeding!\n"); } } void main () { Yelper f = new Fox (); Yelper c = new Cat (); stdout.printf ("// Cat implements Yelper, inherits yelp\n"); stdout.printf ("Cat as Yelper:\n"); (c as Yelper).yelp (); /* Yelper.yelp () */ stdout.printf ("\nCat as Cat:\n"); (c as Cat).yelp (); /* Yelper.yelp () */ stdout.printf ("\n\n// Fox implements Yelper, overrides yelp ()\n"); stdout.printf ("Fox as Yelper:\n"); (f as Yelper).yelp (); /* Fox.yelp () */ stdout.printf ("\nFox as Fox:\n"); (f as Fox).yelp (); /* Fox.yelp () */ } ``` -------------------------------- ### Interface Implementation Example in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/interfaces.rst Demonstrates defining an interface with an abstract method and implementing it across multiple classes with varying inheritance and method overriding strategies. Shows how casting affects method resolution. ```vala /* This example gives you a simple interface, Speaker, with - one abstract method, speak It shows you three classes to demonstrate how these and overriding them behaves: - Fox, implementing Speaker - ArcticFox, extending Fox AND implementing Speaker (ArcticFox.speak () replaces superclasses' .speak ()) - RedFox, extending Fox BUT NOT implementing speaker (RedFox.speak () does not replace superclasses' .speak ()) Important notes: - generally an object uses the most specific class's implementation - ArcticFox extends Fox (which implements Speaker) and implements Speaker itself, - ArcticFox defines speak () with new, so even casting to Fox or Speaker still gives you ArcticFox.speak () - RedFox extends from Fox, but DOES NOT implement Speaker - RedFox speak () gives you RedFox.speak () - casting RedFox to Speaker or Fox gives you Fox.speak () */ /* Speaker: extends from GObject */ interface Speaker : Object { /* speak: abstract without a body */ public abstract void speak (); } /* Fox: implements Speaker, implements speak () */ class Fox : Object, Speaker { public void speak () { stdout.printf (" Fox says Ow-wow-wow-wow\n"); } } /* ArcticFox: extends Fox; must also implement Speaker to re-define * inherited methods and use them as Speaker */ class ArcticFox : Fox, Speaker { /* speak: uses 'new' to replace speak () from Fox */ public new void speak () { stdout.printf (" ArcticFox says Hatee-hatee-hatee-ho!\n"); } } /* RedFox: extends Fox, does not implement Speaker */ class RedFox : Fox { public new void speak () { stdout.printf (" RedFox says Wa-pa-pa-pa-pa-pa-pow!\n"); } } void main () { Speaker f = new Fox (); Speaker a = new ArcticFox (); Speaker r = new RedFox (); stdout.printf ("\n\n// Fox implements Speaker, speak ()\n"); stdout.printf ("Fox as Speaker:\n"); (f as Speaker).speak (); /* Fox.speak () */ stdout.printf ("\nFox as Fox:\n"); (f as Fox).speak (); /* Fox.speak () */ stdout.printf ("\n\n// ArcticFox extends Fox, re-implements Speaker and " + "replaces speak ()\n"); stdout.printf ("ArcticFox as Speaker:\n"); (a as Speaker).speak (); /* ArcticFox.speak () */ stdout.printf ("ArcticFox as Fox:\n"); (a as Fox).speak (); /* ArcticFox.speak () */ stdout.printf ("\nArcticFox as ArcticFox:\n"); (a as ArcticFox).speak (); /* ArcticFox.speak () */ stdout.printf ("\n\n// RedFox extends Fox, DOES NOT re-implement Speaker but" + " does replace speak () for itself\n"); stdout.printf ("RedFox as Speaker:\n"); (r as Speaker).speak (); /* Fox.speak () */ stdout.printf ("\nRedFox as Fox:\n"); (r as Fox).speak (); /* Fox.speak () */ stdout.printf ("\nRedFox as RedFox:\ņ"); (r as RedFox).speak (); /* RedFox.speak () */ } ``` -------------------------------- ### Comment Example Source: https://github.com/gnome/vala/blob/main/doc/manual/source/gidl-metadata-format.rst Comments in .metadata files start with a '#' and extend to the end of the line. ```gidl # this is a comment ``` -------------------------------- ### Compile Vala from Repository with Pre-installed valac Source: https://github.com/gnome/vala/blob/main/README.md Clone the Vala repository, run the autogen script, and then compile and install the compiler. This assumes a valac executable is already available in the system's PATH. ```sh git clone https://gitlab.gnome.org/GNOME/vala cd vala ./autogen.sh make && sudo make install ``` -------------------------------- ### Signal Declaration and Usage Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Shows how to declare a signal in a Vala class and the corresponding delegate type for handling the signal. This example sets up a basic signal emission mechanism. ```vala public class Test : Object { public signal void test (int data); } delegate void TestHandler (Test t, int data); void main () { Test t = new Test (); ``` -------------------------------- ### Download and Compile from Vala Bootstrap Module Source: https://github.com/gnome/vala/blob/main/README.md Clone the Vala bootstrap module, touch stamp files, configure the build with a specified prefix, and then compile and install. This method is used when bootstrapping Vala without an existing valac. ```sh git clone https://gitlab.gnome.org/Archive/vala-bootstrap cd vala-bootstrap touch */*.stamp VALAC=/no-valac ./configure --prefix=/opt/vala-bootstrap make && sudo make install ``` -------------------------------- ### Conditional Compilation Example Source: https://github.com/gnome/vala/blob/main/doc/manual/source/preprocessor.rst Demonstrates how to conditionally compile code using the PREPROCESSOR_DEBUG symbol, which can be defined via the '-D' compiler flag. ```vala // Vala preprocessor example public class Preprocessor : Object { /* public instance method */ public void run () { #if PREPROCESSOR_DEBUG // Use "-D PREPROCESSOR_DEBUG" to run this code path stdout.printf ("debug version"); #else // Normally, we run this code path stdout.printf ("production version"); #endif } } /* application entry point */ void main () { var sample = new Preprocessor (); sample.run (); } ``` -------------------------------- ### Vala Class Definition Example Source: https://github.com/gnome/vala/blob/main/doc/manual/source/overview.rst An example of a simple class definition in Vala, demonstrating a field declaration. This code is illustrative and may not be usable out of context. ```vala class MyClass : Object { int field = 1; } ``` -------------------------------- ### Compile Vala from Repository using Bootstrapped valac Source: https://github.com/gnome/vala/blob/main/README.md Clone the Vala repository, specify the path to a bootstrapped valac executable, run the autogen script, and then compile and install. This is used when compiling Vala with a previously bootstrapped version. ```sh git clone https://gitlab.gnome.org/GNOME/vala cd vala VALAC=/opt/vala-bootstrap/bin/valac ./autogen.sh make && sudo make install ``` -------------------------------- ### Basic Metadata Line Example Source: https://github.com/gnome/vala/blob/main/doc/manual/source/gidl-metadata-format.rst A non-comment line consists of a specifier followed by space-separated key-value parameters. ```gidl foo.bar parameter1="value" parameter2="value" ``` -------------------------------- ### Declaring Error Domains and Throwing/Catching Errors in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/errors.rst Demonstrates the declaration of error domains, throwing new errors with messages, and catching specific errors within try-catch-finally blocks. This example illustrates the fundamental error handling flow in Vala. ```vala errordomain ErrorType1 { CODE_1A } errordomain ErrorType2 { CODE_2A } void thrower () throws ErrorType1, ErrorType2 { throw new ErrorType2.CODE_1A ("Error"); } void catcher () throws ErrorType2 { try { thrower (); } catch (ErrorType1 ex) { // Deal with ErrorType1 } finally { // Tidy up } } void main () { try { catcher (); } catch (ErrorType2 ex) { // Deal with ErrorType2 } } ``` -------------------------------- ### Hiding Symbols Example Source: https://github.com/gnome/vala/blob/main/doc/manual/source/gidl-metadata-format.rst Demonstrates how to use specifiers to hide different types of symbols like Types, Functions, and Fields. ```gidl Foo hidden="1" ``` ```gidl some_function hidden="1" ``` ```gidl Foo.bar hidden="1" ``` -------------------------------- ### Wildcard Specifier Example Source: https://github.com/gnome/vala/blob/main/doc/manual/source/gidl-metadata-format.rst Specifiers can use wildcards to apply metadata to multiple items. This example hides the 'klass' field in all types. ```gidl *.klass hidden="1" ``` -------------------------------- ### Vala Contract Programming Violation Example Source: https://github.com/gnome/vala/blob/main/doc/manual/source/methods.rst Illustrates the output when a contract programming condition is violated in Vala. The example shows a CRITICAL message and the default return value when a postcondition fails. ```vala void main () { stdout.printf ("%i\n", method_name (5, 3.0)); } ``` -------------------------------- ### Continue Statement Syntax Source: https://github.com/gnome/vala/blob/main/doc/manual/source/statements.rst The continue statement immediately transfers execution to the nearest enclosing while, do, for, or foreach statement, starting the next iteration. ```vala continue; ``` -------------------------------- ### Basic Enum Declaration Example in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/enumerated-types-enums.rst This snippet demonstrates a basic enum declaration in Vala. It shows the syntax for defining an enum with named values. ```vala // ... ``` -------------------------------- ### Build Specific Vala Release Version Source: https://github.com/gnome/vala/blob/main/README.md Checkout a specific release tag (e.g., 0.56.17) from the Vala repository, clean untracked files, run the autogen script, and then compile and install. This allows building a particular version of Vala. ```sh git checkout 0.56.17 git clean -dfx ./autogen.sh make && sudo make install ``` -------------------------------- ### Vala Generic Interface and Class Declaration and Usage Source: https://github.com/gnome/vala/blob/main/doc/manual/source/generics.rst Demonstrates the declaration of a generic interface 'With' and two classes, 'One' (non-generic, implementing With) and 'Two' (generic, implementing With). Includes instantiation and usage examples in the main function. ```vala public interface With { public abstract void sett (T t); public abstract T gett (); } public class One : Object, With { public int t; public void sett (int t) { this.t = t; } public int gett () { return t; } } public class Two : Object, With { public T t; public void sett (T t) { this.t = t; } public T gett () { return t; } public U u; } void main () { var o = new One (); o.sett (5); stdout.printf ("%d\n", o.t); var t = new Two (); t.sett (5); stdout.printf ("%d\n", t.t); t.u = 5.0f; stdout.printf ("%f\n", t.u); } ``` -------------------------------- ### Applying Attributes in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/attributes.rst Attributes are applied using the [AnnotationName (details-list)] syntax before the declaration they modify. This example shows how to apply the CCode attribute with a specific C name for a variable. ```vala [CCode (cname = "var_c_name")] static int my_var; ``` -------------------------------- ### Hello, World in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/overview.rst The classic "Hello, world" program in Vala. Store this in a .vala file and compile using 'valac'. ```vala int main (string[] args) { stdout.printf ("hello, world\n"); return 0; } ``` -------------------------------- ### Declare Static and Instance Delegates Source: https://github.com/gnome/vala/blob/main/doc/manual/source/delegates.rst Demonstrates the syntax for declaring static and instance delegates. Static delegates require the [CCode (has_target = false)] annotation. ```vala [CCode (has_target = false)] void DelegateName (int a, int b); ``` ```vala void DelegateName (int a, int b); ``` ```vala [CCode (has_target = false)] void DelegateName () throws GLib.Error; ``` -------------------------------- ### Class Instantiation in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/expressions.rst Use the 'new' operator to create an instance of a class. Provide the class name and any required arguments for its creation method. ```vala new type-name ( arguments ) ``` -------------------------------- ### Struct Creation Method Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/structs.rst Illustrates the syntax for defining a struct's creation method, which controls instantiation. Unlike classes, any code can be placed within this method. ```vala struct-creation-method-declaration: [ struct-access-modifier ] struct-name [ **.** creation-method-name ] **(** param-list **)** **{** statement-list **}** ``` -------------------------------- ### Get Accessor Declaration in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Defines a custom getter for a property in Vala. This allows executing specific code when the property's value is retrieved. ```vala [ class-member-visibility-modifier ] **get** **{** statement-list **}** ``` -------------------------------- ### Using Statement Syntax Source: https://github.com/gnome/vala/blob/main/doc/manual/source/namespaces.rst Illustrates the syntax for the 'using' statement, which allows Vala to search specified namespaces when an identifier cannot be resolved locally. This avoids the need for fully qualified names. ```vala using namespace-list ; ``` -------------------------------- ### Partial Class Definition Source: https://github.com/gnome/vala/blob/main/doc/release-notes/vala-0.56.md Shows how to define a class across multiple parts, potentially in different files, using the `partial` keyword. This is useful for managing very large or complex classes. ```vala public partial class Foo : Object { public double bar { get; set; } } public partial class Foo : Initable { public virtual bool init (Cancellable? cancellable = null) { stdout.printf ("hello!\n"); this.bar = 0.56; return true; } } ``` -------------------------------- ### Vala Class Creation Method Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Defines the syntax for declaring class creation methods, which can be used to instantiate classes with specific properties. The convention is to name these methods with 'with_' followed by a description of the properties being set. ```vala [ class-member-visibility-modifier ] class-name [ **.** creation-method-name ] **(** param-list **)** **{** construction-assignments **}** ``` -------------------------------- ### Basic Class Inheritance in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/concepts.rst Demonstrates how a subclass can inherit from a superclass. A SubType instance is-a SuperType instance. ```vala class SuperType { public int act () { return 1; } } class SubType : SuperType { } ``` -------------------------------- ### Instance Property Declaration in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Declares an instance property within a Vala class. Properties can have custom accessors (get, set, construct) and an optional default value. They can be virtual and overridden. ```vala [ class-member-visibility-modifier ] [ class-method-type-modifier ] qualified-type-name property-name **{** accessors [ default-value ] **}** **;** ``` -------------------------------- ### Vala Application Entry Point Definitions Source: https://github.com/gnome/vala/blob/main/doc/manual/source/overview.rst Defines the acceptable signatures for the main entry point method in a Vala application. The method can be a non-instance method within a namespace or class, optionally accepting a string array for command-line arguments and returning an integer status code. ```vala void main () { ... } ``` ```vala int main () { ... } ``` ```vala void main (string[] args) { ... } ``` ```vala int main (string[] args) { ... } ``` -------------------------------- ### Vala Runtime Error with Fatal Log Levels Source: https://github.com/gnome/vala/blob/main/doc/manual/source/methods.rst Demonstrates how setting fatal log levels in Vala can cause a runtime error when a contract programming assertion fails. This example shows the effect of `Log.set_always_fatal`. ```vala Log.set_always_fatal (LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING); stdout.printf ("%i\n", method_name (5, 3.0)); ``` -------------------------------- ### Connect and Disconnect Signals Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Demonstrates connecting a handler to a signal and then disconnecting it. Ensure the handler is correctly defined and passed. ```vala TestHandler h = (t, data) => { stdout.printf ("Data: %d\n", data); }; t.test (1); t.test.connect (h); t.test (2); t.test.disconnect (h); t.test (3); ``` -------------------------------- ### Async Main Function with Yield Source: https://github.com/gnome/vala/blob/main/doc/release-notes/vala-0.56.md Demonstrates using an `async` main function to call asynchronous operations with `yield`. This is useful for I/O-bound tasks within the main program entry point. ```vala async int main (string[] args) { string dir = args.length == 2 ? args[1] : "."; var file = File.new_for_commandline_arg (dir); try { FileEnumerator enumerator = yield file.enumerate_children_async ( "standard::*,time:*", FileQueryInfoFlags.NOFOLLOW_SYMLINKS ); List children = yield enumerator.next_files_async (int.MAX); print ("total %lu\n", children.length ()); foreach (var info in children) { // print ("%26s %24s %10"+int64.FORMAT+" B %s\n", info.get_content_type (), info.get_access_date_time ().to_string (), info.get_size (), info.get_name ()); } } catch (Error e) { printerr ("failed to enumerate files - %s\n", e.message); return 1; } return 0; } ``` -------------------------------- ### Import GLib Namespace Source: https://github.com/gnome/vala/blob/main/doc/manual/source/namespaces.rst Most Vala code depends on members of the GLib namespace. This snippet shows the standard way to import it. ```vala using GLib; ``` -------------------------------- ### Vala Contract Programming with Preconditions and Postconditions Source: https://github.com/gnome/vala/blob/main/doc/manual/source/methods.rst Shows how to use 'requires' for preconditions and 'ensures' for postconditions in Vala methods to enforce contract programming principles. The 'result' variable represents the return value. ```vala double method_name (int x, double d) requires (x > 0 && x < 10) requires (d >= 0.0 && d <= 1.0) ensures (result >= 0.0 && result <= 10.0) { return d * x; } ``` -------------------------------- ### Instance Delegate with Class Method Source: https://github.com/gnome/vala/blob/main/doc/manual/source/delegates.rst Illustrates creating an instance delegate assigned to a class method. The 'this' keyword is implicitly available within the invoked method. ```vala class Test : Object { private int data = 5; public void method (int a) { stdout.printf ("%d %d\n", a, this.data); } } delegate void DelegateType (int a); void main () { var t = new Test (); DelegateType d = t.method; d (1); } ``` -------------------------------- ### Define a scope with local variables in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/concepts.rst Illustrates a basic Vala method 'main' with local variable declarations and an arithmetic expression. ```vala void main () { int a = 5; int b = a + 1; } ``` -------------------------------- ### Struct Instantiation in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/expressions.rst Instantiate a struct by providing its name and optional arguments. An initializer block can be used to set field values. ```vala type-name ( arguments ) { initializer } ``` -------------------------------- ### Invoke Delegate and Pass as Parameter Source: https://github.com/gnome/vala/blob/main/doc/manual/source/delegates.rst Shows how to invoke a delegate and pass it as a parameter to another method. The delegate is called using its variable name. ```vala void f1 (int a) { stdout.printf ("%d\n", a); } ... void f2 (DelegateType d, int a) { d (a); } ... f2 (f1, 5); ``` -------------------------------- ### Nested Namespace Declarations Source: https://github.com/gnome/vala/blob/main/doc/manual/source/namespaces.rst Demonstrates two ways to declare nested namespaces: by nesting declarations or by using dot notation in a single declaration. ```vala namespace NameSpaceName1 { namespace NameSpaceName2 { } } namespace NameSpaceName1.NameSpaceName2 { } ``` -------------------------------- ### Simple Class Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst The most basic way to declare a class in Vala. ```vala class ClassName { } ``` -------------------------------- ### Virtual Properties in a Class Hierarchy Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Demonstrates the use of virtual properties in Vala, showing a base class with a virtual property and subclasses that either use the default implementation or override it. ```vala namespace Properties { class Base : Object { protected int _number; public virtual int number { get { return this._number; } set { this._number = value; } } } /** * This class just use Base class default handle * of number property. */ class Subclass : Base { public string name { get; set; } } /** * This class override how number is handle internally. */ class ClassOverride : Base { public override int number { get { return this._number; } set { this._number = value * 3; } } } void main () { stdout.printf ("Implementing Virtual Properties...\n"); var bc = new Base (); bc.number = 3; stdout.printf ("Class number = '" + bc.number.to_string () + "'\n"); var sc = new Subclass (); sc.number = 3; stdout.printf ("Class number = '" + sc.number.to_string () + "'\n"); var co = new ClassOverride (); co.number = 3; stdout.printf ("Class number = '" + co.number.to_string () + "'\n"); } } ``` -------------------------------- ### Class Delegate Declaration Syntax Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Illustrates the syntax for defining a delegate within a class. Similar to namespace delegates, it supports class member visibility. ```vala class-delegate-declaration: [ class-member-visibility-modifier ] return-type delegate delegate-name ( method-params-list ); ``` -------------------------------- ### Vala Class Instance Constructor Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Specifies the syntax for an instance constructor block. Code within this block is executed on every instance of the class after construction properties have been set. ```vala **construct** **{** statement-list **}** ``` -------------------------------- ### Abstract Properties in a Class Hierarchy Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Illustrates abstract properties in Vala, showcasing an abstract base class with an abstract property and a concrete subclass that implements it. The 'construct' keyword is used for initialization. ```vala namespace Properties { abstract class Base : Object { public abstract string name { get; set construct; } construct { this.name = "NO_NAME"; } } class Subclass : Base { private string _name; public override string name { get { return this._name; } set construct { this._name = value; } } /* This class have a default constructor that initializes * name as the construct block on Base, and a .with_name() * constructor where the user can set class derived name * property. */ public Subclass.with_name (string name) { Object (name:name); this._name = name; } } void main () { stdout.printf ("Implementing Abstract Properties...\n"); var sc = new Subclass.with_name ("TEST_CLASS"); stdout.printf ("Class name = '" + sc.name + "'\n"); var sc2 = new Subclass (); stdout.printf ("Class name = '" + sc2.name + "'\n"); } } ``` -------------------------------- ### Delegate Invocation with Lambda Expression Source: https://github.com/gnome/vala/blob/main/doc/manual/source/delegates.rst Demonstrates using a lambda expression to define the implementation for a delegate invocation, providing a concise inline method. ```vala f2 (a => { stdout.printf ("%d\n", a); }, 5); ``` -------------------------------- ### Array Instantiation in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/expressions.rst Create an array of a specified type and size. Initializer expressions can be provided to populate the array upon creation. ```vala new type-name [ sizes ] { initializer } { initializer } ``` -------------------------------- ### Vala Class Class Constructor Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Defines the syntax for a class constructor block. This code executes once at the first use of the class and once at the first use of each subclass. ```vala **class** **construct** **{** statement-list **}** ``` -------------------------------- ### Vala Class Static Constructor Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Specifies the syntax for a static constructor block. This code runs exactly once in a program the first time a class or any of its subclasses are instantiated. ```vala **static** **construct** **{** statement-list **}** ``` -------------------------------- ### Vala Increment/Decrement Operations Source: https://github.com/gnome/vala/blob/main/doc/manual/source/expressions.rst Demonstrates equivalent postfix and prefix increment/decrement operations in Vala. Use these for modifying variable values. ```vala var postfix = i++; var prefix = --j; ``` ```vala var postfix = i; i += 1; j -= 1; var prefix = j; ``` -------------------------------- ### Interface Instance Method Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/interfaces.rst Syntax for declaring an instance method within a Vala interface. These methods can be abstract or have implementations. ```vala [ class-member-visibility-modifier ] return-type method-name ( [ params-list ] ) method-contracts [ throws exception-list ] { statement-list } ``` -------------------------------- ### Basic Namespace Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/namespaces.rst Declares a simple namespace. Use this to create a distinct scope for your definitions. ```vala namespace NameSpaceName { } ``` -------------------------------- ### Demonstrating Method Overriding and Polymorphism in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/concepts.rst Illustrates how method overriding works with inheritance. The invoked method depends on the type the invoker believes it is dealing with. ```vala SubType sub = new SubType (); SuperType super = sub; sub.act (); super.act (); ``` -------------------------------- ### Struct Method Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/structs.rst Outlines the syntax for declaring methods within a struct. Methods can be static and may have specified return types, parameters, contracts, and exceptions. ```vala struct-method-declaration: [ access-modifier ] [ struct-method-type-modifier ] return-type method-name **(** [ params-list ] **)** method-contracts [ **throws** exception-list ] **{** statement-list **}** ``` -------------------------------- ### Dynamic Signal Invocation and Access Source: https://github.com/gnome/vala/blob/main/doc/release-notes/vala-0.56.md Demonstrates using the `dynamic` keyword to access object properties and invoke signals that might not be known at compile time. Signals can be emitted using `.emit()`. ```vala dynamic Gst.Element appsink = Gst.ElementFactory.make ("appsink", "my_src"); appsink.max_buffers = 0; appsink.eos.connect (on_eos); Gst.Sample? res = appsink.pull_sample.emit (); ``` -------------------------------- ### Vala Inline Method (Lambda) Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/methods.rst Demonstrates how to declare an inline method using lambda syntax and assign it to a delegate type. This is a more succinct way to define methods that can be assigned to variables. ```vala delegate int DelegateType (int a, string b); int use_delegate (DelegateType d, int a, string b) { return d (a, b); } int make_delegate () { DelegateType d = (a, b) => { return a; }; use_delegate (d, 5, "test"); } ``` -------------------------------- ### Vala Class Instance Destructor Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Shows the syntax for declaring a class instance destructor. The documentation notes that the timing and behavior of destruction depend on the class type. ```vala **~** class-name **(** **)** **{** statement-list **}** ``` -------------------------------- ### Declare and initialize a string variable in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/concepts.rst Declares a string variable 's' and initializes it with the value 'stringvalue'. The variable takes ownership of the new instance. ```vala string s = "stringvalue"; ``` -------------------------------- ### Class Declaration Syntax Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Defines the general structure for declaring a class in Vala, including optional access modifiers, inheritance lists, and class members. ```vala class ClassName : GLib.Object { public ClassName () { } public ClassName.with_some_quality (Property1Type property1value) { this.property1 = property1value; } } ``` -------------------------------- ### Interface Abstract Instance Method Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/interfaces.rst Syntax for declaring an abstract instance method in a Vala interface. Implementing classes must provide a concrete implementation. ```vala [ class-member-visibility-modifier ] abstract return-type method-name ( [ params-list ] ) method-contracts [ throws exception-list ] ; ``` -------------------------------- ### Struct Field Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/structs.rst Shows the syntax for declaring fields within a struct. Fields can be static and may be initialized with an expression. ```vala struct-field-declaration: [ access-modifier ] [struct-field-type-modifier] qualified-type-name field-name [ **=** expression ] ; ``` -------------------------------- ### Explicit Global Namespace Reference Source: https://github.com/gnome/vala/blob/main/doc/manual/source/namespaces.rst Shows how to explicitly refer to an identifier in the global namespace using the 'global::' prefix. This is useful when a local variable shares a name with a global identifier. ```vala global::identifier ``` -------------------------------- ### Class Member Scope Resolution Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Demonstrates using 'this' to disambiguate between a local parameter and an instance field with the same name within a class method. ```vala class ClassName { int field_name; void function_name (field_name) { this.field_name = field_name; } } ``` -------------------------------- ### Vala Preprocessor Syntax Overview Source: https://github.com/gnome/vala/blob/main/doc/manual/source/preprocessor.rst Defines the general syntax for Vala preprocessor directives, including conditional compilation structures like #if, #elif, and #else, along with expression parsing. ```vala [ any vala code ] [ pp-condition ] [ any vala code ] pp-condition: #if pp-expression vala code [ pp-elif ] [ pp-else ] #endif pp-elif: #elif pp-expression vala code [ pp-elif ] pp-else: #else vala code pp-expression: pp-or-expression pp-or-expression: pp-and-expression [ || pp-and-expression ] pp-and-expression: pp-binary-expression [ && pp-binary-expression ] pp-binary-expression: pp-equality-expression pp-inequality-expression pp-equality-expression: pp-unary-expression [ == pp-unary-expression ] pp-inequality-expression: pp-unary-expression [ != pp-unary-expression ] pp-unary-expression: pp-negation-expression pp-primary-expression pp-negation-expression: ! pp-unary-expression pp-primary-expression: pp-symbol ( pp-expression ) true false pp-symbol: identifier ``` -------------------------------- ### Nested Functions for Code Structure Source: https://github.com/gnome/vala/blob/main/doc/release-notes/vala-0.56.md Illustrates nested functions, which are local to their containing function and can access its variables. This helps in structuring complex functions and reusing callbacks. ```vala void write_streams (OutputStream stream1, OutputStream stream2, uint8[] data) { void nested_function (Object object_source, AsyncResult result) { OutputStream stream = object_source as OutputStream; try { ssize_t size = stream.write_async.finish (result); stdout.printf (@"Written $size bytes\n"); } catch (Error e) { stderr.printf ("Error writing to stream: %s", e.message); } } stream1.write_async (data, nested_function); stream2.write_async (data, nested_function); } ``` -------------------------------- ### Return Statement Syntax Source: https://github.com/gnome/vala/blob/main/doc/manual/source/statements.rst The return statement ends the execution of a method. If an expression is provided, its value becomes the return value of the method invocation. ```vala return [ expression ]; ``` -------------------------------- ### Custom Array Length Type Source: https://github.com/gnome/vala/blob/main/doc/release-notes/vala-0.56.md Demonstrates how to specify a custom integer type (e.g., uint8) for the length of an array when declaring it. ```vala string[] list = new string[10:uint8]; ``` -------------------------------- ### Basic Struct Declaration Source: https://github.com/gnome/vala/blob/main/doc/manual/source/structs.rst Defines a simple struct with a single integer field. A struct must have at least one field unless specific conditions are met. ```vala struct StructName { int some_field; } ``` -------------------------------- ### Throw Statement Syntax Source: https://github.com/gnome/vala/blob/main/doc/manual/source/statements.rst The throw statement is used to raise an exception during program execution. ```vala throw expression; ``` -------------------------------- ### Abstract Instance Method Declaration in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/classes.rst Declares an abstract instance method in a Vala class. Abstract methods have no implementation and must be overridden by concrete subclasses. They can only exist in abstract classes. ```vala [ class-member-visibility-modifier ] **abstract** return-type method-name **(** [ params-list ] **)** method-contracts [ **throws** exception-list ] **;** ``` -------------------------------- ### Basic Interface Declaration in Vala Source: https://github.com/gnome/vala/blob/main/doc/manual/source/interfaces.rst Defines the simplest possible interface structure in Vala. Interfaces are non-instantiable types. ```vala interface InterfaceName { } ```