KINDS
Library
6 min
a library 's primary use is being a collection of methods a library can also contain readonly fields this is most closely equivilant to a static class in c# with readonly fields or init properties a library is not a type, it is a value the major implication is you cannot create a new instance of it add explaination of what type a library is add explaination of what type a library is declaration to declare a new library define a top level construct with the keyword library followed by the desired identifier the name must be unique within the corresponding namespace docid gwa7zz7fnle1mvm4rbhn a library contains any number of named methods docid\ gdytda2qp1ivc7i30nt2n readonly fields docid\ sz10ih owphmjgswgtavc shift library examplelibrary //physically located in /libraries { double sevenpointfive = 7 5; double zero; complextype defaultstate; int double(int foo) { return foo 2; } string makesuper(string phrase) { return $"super {phrase}"; } //lambda syntax for method declaration is supported //see aspects/methods for more information bool alwaysfalse => false; bool inverse (bool x) => !x; int usemembers(int x) { return x sevenpointfive; } int hiddenscope(string sevenpointfive) { return sevenpointfive length() this sevenpointfive; } } remarks library fields are readonly and cannot be changed that means a non initialized field will be left with it's default value all members are automatically scoped into methods during execution the implication being that you can use these within methods see usemembers method in the above example for a demostration of this method parameters will take scope precedence and can hide local members you can access any hidden fields via the this keyword check out the hidden scope function libraries cannot have constructors as they are not initialized this understandably impacts the complexity of scoped fields immutable value c# equivalent using system; using exampleapp complextypeclasses; namespace exampleapp libraries { public static class examplelibrary { public static double sevenpointfive = 7 5; double zero; complextype defaultstate; public static int double(int foo) { return foo 2; } public static string makesuper(string phrase) { return $"super {phrase}"; } public static bool alwaysfalse => false; public static bool inverse (bool x) => !x; public static int usemembers(int x) { return x sevenpointfive; } public static int hiddenscope (string sevenpointfive) { return sevenpointfive length() this sevenpointfive; } } } usage a library's aspects are directly exposed as aspects on the library's type identifier using our example the type identifier is examplelibrary as usual, we use the member access operator so to access member makesuper you would type examplelibrary makesuper in practice consume of the library might look like this //a scope with statements var foo = 5; var saiyan = "saiyan"; var doubled = examplelibrary double(foo); var supersaiyan = examplelibrary makesuper(saiyan); var doublemethod = examplelibrary double; //https //en wikipedia org/wiki/first class function var alsodoubled = doublemethod(5); console writeline(doubled == alsodoubled);//outputs true console writeline(doublemethod == examplelibrary double);//outputs true console writeline(examplelibrary sevenpointfive);//outputs 7 5 var libraryinstance = new examplelibrary(); //compilation error because libraries cannot be initialized remarks the primary thing to call attention to is while the declaration has significantly less code the usage is very similiar, sometimes identical this is in support of the approachability design pillar
