Method calls occurs as an important part of object paths in expressions, and in standalone mode, as call statements.
Object paths represents navigation among a network of objects and references to objects. A method call is simply a subset of all possible method paths, specifically, those that end executing a method. Nevertheless, there may be one or more additional method calls embedded in the middle of the object path.
All object paths must start with a known object: a local variable reference, an argument reference, a static member reference or a literal constant. For instance:
Application.OpenForms[0].Text args[0].SubString(0, 5) Self.symbol.Owner.Declaration.MarkAutomaticFields symbol.Owner.Declaration.MarkAutomaticFields
The first object path starts with the static member OpenForms from the Application class. The second object path starts with some entity called args: it could represent the input parameter from the Main application entry method. The third path starts with Self, which most of the times is the implicit object in an instance method execution. Note that the last path is equivalent to the third one, except that Self is omitted.
Once you have a first step in a path, you can keep extending it by adding the following operations:
Step | Meaning |
---|---|
path . identifier | Member access |
path ( arguments ) | Method call |
path [ indexes ] | Array indexing or indexer call |
You must be careful, however, while interpreting an object path: a dot can also be used to separate namespace identifiers, and brackets can be used to set type arguments for generic method calls.
Type inference is a compiler technique borrowed from functional languages and applicable to generic method calls. Let's suppose we have this generic method definition:
MathTools = static class public method Min[X(IComparable)](A, B: X); // ... end;
We could use the previous generic method by using an explicit list of type arguments, like this:
Console.WriteLine(MathTools.Min[Integer](1, 2)); Console.WriteLine(MathTools.Min[Double](1, 2));
We could drop explicit type arguments to allow the compiler to infer the required type arguments:
Console.WriteLine(MathTools.Min(1, 2)); Console.WriteLine(MathTools.Min[Double](1.0, 2.0));
Please note, however, that type inference is an extra facility provided by the compiler for your comfort, and it may fail in some cases. For instance, the compiler would signal an error with this method call:
Console.WriteLine(MathTools.Min(1, 2.0));
The compiler infers that the generic type parameter should be an integer, from the first supplied method parameter. Then, the second parameter suggests the type parameter should be a double. Though there is an implicit conversion from integers to doubles, both Freya and C# compilers reject this method call. Otherwise, it may introduce hard to detect bugs. Type inference, taken to the extreme, may compromise the security of type systems.
The Freya Programming Language
Statements
Assignment statements
Increment/decrement statements
Expressions
Methods
Generic methods