It is possible to bind values to names using @:
struct Badger {
pub age: u8
}
fn main() {
// Let's create a Badger instances
let badger_john = Badger { age: 8 };
// Now try to find out what John's favourite activity is, based on his age
match badger_john.age {
...
// Create a boolean value
let a = true;
// The following expression will try and find a pattern for our value starting with
// the topmost pattern.
// This is an exhaustive match expression because it checks for every possible value
match a {
true => println!("a is true"),
...
It's possible to treat multiple, distinct values the same way, using |:
enum Colour {
Red,
Green,
Blue,
Cyan,
Magenta,
Yellow,
Black
}
enum ColourModel {
RGB,
CMYK
}
// let's take an example colour
let colour = Colour::Red;
let model = match ...
Patterns can be matched based on values independent to the value being matched using if guards:
// Let's imagine a simplistic web app with the following pages:
enum Page {
Login,
Logout,
About,
Admin
}
// We are authenticated
let is_authenticated = true;
// But we aren't admins...
// View to hold the CAGradientLayer.
let view: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 320))
// Initialize gradient layer.
let gradientLayer: CAGradientLayer = CAGradientLayer()
// Set frame of gradient layer.
gradientLayer.frame = view.bounds
// Color at...
public class Singleton
{
private readonly static Singleton instance = new Singleton();
private Singleton() { }
public static Singleton Instance => instance;
}
This implementation is thread-safe because in this case instance object is initialized in the static constructor. The ...
This thread-safe version of a singleton was necessary in the early versions of .NET where static initialization was not guaranteed to be thread-safe. In more modern versions of the framework a statically initialized singleton is usually preferred because it is very easy to make implementation mistak...
When assigning named methods to delegates, they will refer to the same underlying object if:
They are the same instance method, on the same instance of a class
They are the same static method on a class
public class Greeter
{
public void WriteInstance()
{
Console.Write...
The following syntax creates a delegate type with name NumberInOutDelegate, representing a method which takes an int and returns an int.
public delegate int NumberInOutDelegate(int input);
This can be used as follows:
public static class Program
{
static void Main()
{
Number...
Named methods can be assigned to delegates with matching signatures:
public static class Example
{
public static int AddOne(int input)
{
return input + 1;
}
}
Func<int,int> addOne = Example.AddOne
Example.AddOne takes an int and returns an int, its signature ...
Lambdas can be used to create anonymous methods to assign to a delegate:
Func<int,int> addOne = x => x+1;
Note that the explicit declaration of type is required when creating a variable this way:
var addOne = x => x+1; // Does not work
PHP will generally correctly guess the data type you intend to use from the context it's used in, however sometimes it is useful to manually force a type. This can be accomplished by prefixing the declaration with the name of the required type in parenthesis:
$bool = true;
var_dump($bool); // bool...
In Python 2, exec is a statement, with special syntax: exec code [in globals[, locals]]. In Python 3 exec is now a function: exec(code, [, globals[, locals]]), and the Python 2 syntax will raise a SyntaxError.
As print was changed from statement into a function, a __future__ import was also added. ...
On either Linux/UNIX or Windows, a script can be passed as an argument to the PHP executable, with that script's options and arguments following:
php ~/example.php foo bar
c:\php\php.exe c:\example.php foo bar
This passes foo and bar as arguments to example.php.
On Linux/UNIX, the preferred me...
In Python 2, when a property raise a error, hasattr will ignore this property, returning False.
class A(object):
@property
def get(self):
raise IOError
class B(object):
@property
def get(self):
return 'get in b'
a = A()
b = B()
print 'a hasattr get:...
int getListOfFriends(size_t size, int friend_indexes[]) {
size_t i = 0;
for (; i < size; i++) {
friend_indexes[i] = i;
}
}
C99C11
/* Type "void" and VLAs ("int friend_indexes[static size]") require C99 at least.
In C11 VLAs are optional. */
void getLis...
Leiningen
Note: If you're going to use Leiningen, you first need to download and install JDK 6 or newer.
The easiest way to get started with Clojure is to download and install Leiningen, the de facto standard tool to manage Clojure projects.
Linux:
curl https://raw.githubusercontent.com/technoma...
When in a loop, the loop variable (val) in the following example is a single variable that changes value as it goes over the loop. Therefore one must do the following to actually pass each val of values to the goroutine:
for val := range values {
go func(val interface{}) {
fmt.Println...