Tutorial by Examples: am

Using reflection, a method of an object can be invoked during runtime. The example shows how to invoke the methods of a String object. import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; String s = "Hello World!"; // method without parameters // i...
Often, we need to perform an operation over every element in a variadic template parameter pack. There are many ways to do this, and the solutions get easier to read and write with C++17. Suppose we simply want to print every element in a pack. The simplest solution is to recurse: C++11 void print...
Here is some sample XML against which example XPaths can be written: <r> <e a="1"/> <f a="2" b="1">Text 1</f> <f/> <g> <i c="2">Text 2</i> Text 3 <j>Text 4</j> </g&gt...
def say_hello_to(name) puts "Hello #{name}" end say_hello_to('Charles') # Hello Charles
def greet(greeting, name) puts "#{greeting} #{name}" end greet('Hi', 'Sophie') # Hi Sophie
def make_animal_sound(sound = 'Cuack') puts sound end make_animal_sound('Mooo') # Mooo make_animal_sound # Cuack It's possible to include defaults for multiple arguments: def make_animal_sound(sound = 'Cuack', volume = 11) play_sound(sound, volume) end make_animal_so...
def welcome_guests(*guests) guests.each { |guest| puts "Welcome #{guest}!" } end welcome_guests('Tom') # Welcome Tom! welcome_guests('Rob', 'Sally', 'Lucas') # Welcome Rob! # Welcome Sally! # W...
First install Mono by going through the install instructions for the platform of your choice as described in their installation section. Mono is available for Mac OS X, Windows and Linux. After installation is done, create a text file, name it HelloWorld.cs and copy the following content into it: ...
A common pattern in C, to easily imitate returning multiple values from a function, is to use pointers. #include <stdio.h> void Get( int* c , double* d ) { *c = 72; *d = 175.0; } int main(void) { int a = 0; double b = 0.0; Get( &a , &b ); pr...
This Makefile will cross compile and zip up executables for Windows, Mac and Linux (ARM and x86). # Replace demo with your desired executable name appname := demo sources := $(wildcard *.go) build = GOOS=$(1) GOARCH=$(2) go build -o build/$(appname)$(3) tar = cd build && tar -cvzf $...
A namespace declaration can look as follows: namespace MyProject; - Declare the namespace MyProject namespace MyProject\Security\Cryptography; - Declare a nested namespace namespace MyProject { ... } - Declare a namespace with enclosing brackets. It is recommended to only declare a single na...
As shown in Declaring Namespaces, we can define a class in a namespace as follows: namespace MyProject\Shapes; class Rectangle { ... } To reference this class the full path (including the namespace) needs to be used: $rectangle = new MyProject\Shapes\Rectangle(); This can be shortened by ...
When defining discriminated unions you can name elements of tuple types and use these names during pattern matching. type Shape = | Circle of diameter:int | Rectangle of width:int * height:int let shapeIsTenWide = function | Circle(diameter=10) | Rectangle(width=10) -> t...
Example: Get a Stream of 30 elements, containing 21st to 50th (inclusive) element of a collection. final long n = 20L; // the number of elements to skip final long maxSize = 30L; // the number of elements the stream should be limited to final Stream<T> slice = collection.stream().skip(n).li...
Variable declaration for examples: Collection<String> abc = Arrays.asList("a", "b", "c"); Collection<String> digits = Arrays.asList("1", "2", "3"); Collection<String> greekAbc = Arrays.asList("alpha", "be...
Java does not have a Char Stream, so when working with Strings and constructing a Stream of Characters, an option is to get a IntStream of code points using String.codePoints() method. So IntStream can be obtained as below: public IntStream stringToIntStream(String in) { return in.codePoints(); ...
3.0 In Swift 3 there are multiple access-levels. This example uses them all except for open: public struct Car { public let make: String let model: String //Optional keyword: will automatically be "internal" private let fullName: String fileprivate var otherName...
public class SuperClass { private func secretMethod() {} } internal class SubClass: SuperClass { override internal func secretMethod() { super.secretMethod() } }
struct Square { private(set) var area = 0 var side: Int = 0 { didSet { area = side*side } } } public struct Square { public private(set) var area = 0 public var side: Int = 0 { didSet { area = side*side } ...
class Animal def method_missing(method, *args, &block) say, speak = method.to_s.split("_") if say == "say" && speak return speak.upcase if args.first == "shout" speak else super end end end => Animal.new....

Page 11 of 129