Tutorial by Examples: const

class Message DEFAULT_MESSAGE = "Hello, world" def speak(message = nil) if message puts message else puts DEFAULT_MESSAGE end end end The constant DEFAULT_MESSAGE can be changed with the following code: Message::DEFAULT_MESSAGE = "Hullo, wo...
Example: Invoke different constructors by passing relevant parameters import java.lang.reflect.*; class NewInstanceWithReflection{ public NewInstanceWithReflection(){ System.out.println("Default constructor"); } public NewInstanceWithReflection( String a){ ...
All constructors in Java must make a call to the Object constructor. This is done with the call super(). This has to be the first line in a constructor. The reason for this is so that the object can actually be created on the heap before any additional initialization is performed. If you do not spe...
Classes without dependencies can easily be created by dagger. public class Engine { @Inject // <-- Annotate your constructor. public Engine() { } } This class can be provided by any component. It has no dependencies itself and is not scoped. There is no further code necessar...
If you want to create an instance of an inner nested class you need to provide a class object of the enclosing class as an extra parameter with Class#getDeclaredConstructor. public class Enclosing{ public class Nested{ public Nested(String a){ System.out.println("Constru...
HVFL is a language designed to constrain UI elements in a simple and quick fashion. Generally, VFL has an advantage over traditional UI customization in the Interface Builder because it's much more readable, accessible and compact. Here's an example of VFL, in which three UIViews are constrained f...
Well, if you want a switch/case construct, the most straightforward way to go is to use the good old if/else construct: def switch(value): if value == 1: return "one" if value == 2: return "two" if value == 42: return "the answer to...
In C, character constants and string literals are different things. A character surrounded by single quotes like 'a' is a character constant. A character constant is an integer whose value is the character code that stands for the character. How to interpret character constants with multiple charac...
If the type on which the static constructor is declared is generic, the static constructor will be called once for each unique combination of generic arguments. class Animal<T> { static Animal() { Console.WriteLine(typeof(T).FullName); } public static void Yawn...
If a static constructor throws an exception, it is never retried. The type is unusable for the lifetime of the AppDomain. Any further usages of the type will raise a TypeInitializationException wrapped around the original exception. public class Animal { static Animal() { Consol...
x IN ( SELECT ... ) turn into a JOIN When possible, avoid OR. Do not 'hide' an indexed column in a function, such as WHERE DATE(x) = ...; reformulate as WHERE x = ... You can generally avoid WHERE LCASE(name1) = LCASE(name2) by having a suitable collation. Do no use OFFSET for "paginatio...
A very useful feature many people overlook is the ability to construct a Map using a SOQL query. Map<Id, Account> accounts = new Map<Id, Account>([SELECT Id, Name FROM Account]); System.debug(accounts); When you run this code, accounts then contains a Map of your Account objects, ke...
The $q constructor function is used to create promises from asynchronous APIs that use callbacks to return results. $q(function(resolve, reject) {...}) The constructor function receives a function that is invoked with two arguments, resolve and reject that are functions which are used to eithe...
To use the constant simply use its name: if (EARTH_IS_FLAT) { print "Earth is flat"; } print APP_ENV_UPPERCASE; or if you don't know the name of the constant in advance, use the constant function: // this code is equivalent to the above code $const1 = "EARTH_IS_FLAT&quo...
Say you want to add a foreign key company_id to the users table, and you want to have a NOT NULL constraint on it. If you already have data in users, you will have to do this in multiple steps. class AddCompanyIdToUsers < ActiveRecord::Migration def up # add the column with NULL allowed ...
Pi The following returns the value of PI formatted to 6 decimal places. The actual value is good to DOUBLE; SELECT PI(); -> 3.141593
We can create a Map from a list of tuples like this: Map.fromList [("Alex", 31), ("Bob", 22)] A Map can also be constructed with a single value: > Map.singleton "Alex" 31 fromList [("Alex",31)] There is also the empty function. empty :: Map k a ...
Constructors are special methods named after the class and without a return type, and are used to construct objects. Constructors, like methods, can take input parameters. Constructors are used to initialize objects. Abstract classes can have constructors also. public class Hello{ // construct...
A function that is declared constexpr is implicitly inline and calls to such a function potentially yield constant expressions. For example, the following function, if called with constant expression arguments, yields a constant expression too: C++11 constexpr int Sum(int a, int b) { return ...
Prior to C++17, template deduction cannot deduce the class type for you in a constructor. It must be explicitly specified. Sometimes, however, these types can be very cumbersome or (in the case of lambdas) impossible to name, so we got a proliferation of type factories (like make_pair(), make_tuple(...

Page 8 of 13