Tutorial by Examples: arguments

We can wrap a class decorator with another function to allow customization: function addMetadata(metadata: any) { return function log(target: any) { // Add metadata target.__customMetadata = metadata; // Return target return target; ...
To validate arguments to methods called on a mock, use the ArgumentCaptor class. This will allow you to extract the arguments into your test method and perform assertions on them. This example tests a method which updates the name of a user with a given ID. The method loads the user, updates the na...
Ideally, Prolog predicates can be used in all directions. For many pure predicates, this is also actually the case. However, some predicates only work in particular modes, which means instantiation patterns of their arguments. By convention, the most common argument order for such predicates is: ...
Prior to C++17, when writing a template non-type parameter, you had to specify its type first. So a common pattern became writing something like: template <class T, T N> struct integral_constant { using type = T; static constexpr T value = N; }; using five = integral_constant&l...
Mockito provides a Matcher<T> interface along with an abstract ArgumentMatcher<T> class to verify arguments. It uses a different approach to the same use-case than the ArgumentCaptor. Additionally the ArgumentMatcher can be used in mocking too. Both use-cases make use of the Mockito.argT...
For functions that need to take a collection of objects, slices are usually a good choice: fn work_on_bytes(slice: &[u8]) {} Because Vec<T> and arrays [T; N] implement Deref<Target=[T]>, they can be easily coerced to a slice: let vec = Vec::new(); work_on_bytes(&vec); le...
Some procedures have optional arguments. Optional arguments always come after required arguments, but the procedure can be called without them. For example, if the function, ProcedureName were to have two required arguments (argument1, argument2), and one optional argument, optArgument3, it could b...
Lets assume we have this class and we would like to test doSmth method. In this case we want to see if parameter "val" is passed to foo. Object foo is mocked. public class Bar { private final Foo foo; public Bar(final Foo foo) { this.foo = foo; } public ...
You can combine named arguments with optional parameters. Let see this method: public sealed class SmsUtil { public static bool SendMessage(string from, string to, string message, int retryCount = 5, object attachment = null) { // Some code } } When you want to call t...
Batch file command line arguments are parameter values submitted when starting the batch. They should be enclosed in quotes if they contain spaces. In a running batch file, the arguments are used for various purposes, i.e. redirection to :labels, setting variables, or running commands. The argument...
When more than 9 arguments are supplied, the shift [/n] command can be used, where /n means start at the nth argument, n is between zero and eight. Looping through arguments: :args set /a "i+=1" set arg!i!=%~1 call echo arg!i! = %%arg!i!%% shift goto :args Note, in the above exam...
Arrow functions do not expose an arguments object; therefore, arguments would simply refer to a variable in the current scope. const arguments = [true]; const foo = x => console.log(arguments[0]); foo(false); // -> true Due to this, arrow functions are also not aware of their caller/ca...
Consider following is our function call. FindArea(120, 56); In this our first argument is length (ie 120) and second argument is width (ie 56). And we are calculating the area by that function. And following is the function definition. private static double FindArea(int length, int width) ...
Consider preceding is our function definition with optional arguments. private static double FindAreaWithOptional(int length, int width=56) { try { return (length * width); } catch (Exception) { thro...
C99 Macros with variadic args: Let's say you want to create some print-macro for debugging your code, let's take this macro as an example: #define debug_print(msg) printf("%s:%d %s", __FILE__, __LINE__, msg) Some examples of usage: The function somefunc() returns -1 if failed and 0 ...
You define a keyword argument in a method by specifying the name in the method definition: def say(message: "Hello World") puts message end say # => "Hello World" say message: "Today is Monday" # => "Today is Monday" You can define multip...
2.1 Required keyword arguments were introduced in Ruby 2.1, as an improvement to keyword arguments. To define a keyword argument as required, simply declare the argument without a default value. def say(message:) puts message end say # => ArgumentError: missing keyword: message say ...
You can define a method to accept an arbitrary number of keyword arguments using the double splat (**) operator: def say(**args) puts args end say foo: "1", bar: "2" # {:foo=>"1", :bar=>"2"} The arguments are captured in a Hash. You can manip...
Use the curry function (from Prelude or Data.Tuple) to convert a function that takes tuples to a function that takes two arguments. curry fst 1 2 -- computes 1 curry snd 1 2 -- computes 2 curry (uncurry f) -- computes the same as f import Data.Tuple (swap) curry swap 1 2 -- computes (2, 1...
If a function has multiple arguments, it is unspecified what order they are evaluated in. The following code could print x = 1, y = 2 or x = 2, y = 1 but it is unspecified which. int f(int x, int y) { printf("x = %d, y = %d\n", x, y); } int get_val() { static int x = 0; r...

Page 4 of 6