Functions

The language Dart is a true object-oriented language which has the functionality of functions. A function in Dart is an own object structure which refers to the Function class. The fact that a functions is an object has some advantages. It can be assigned to variables or passed as an argument to other functions.

Basic function:

The API has an own structure to represent a function from Dart. To start the creation of an function you should use the companion functions in the `FunctionSpec` class. This object has only one method to create a new instance of an builder which can be used for the definition of an function. The builder class provide some functions which allows the user to describe details about the function which should be created.

The creation of functions needs the knowledge about which different parameter types the language has

Let's create an function which prints a message into the log:

FunctionSpec.builder("helloWorld")
    .returns(Void::class) // It is not necessary because void is the default return type
    .addCode("debugPrint('Hello World');")
     .build()

Shorthand (=>) expression:

The language has the ability to use an shorthand expression for the `{ return expr; }` syntax. This has some limitations in the usage which are comes directly from the Dart wiki itself:

Only expressions can appear between the arrow (=>) and the semicolon (;). Expressions evaluate to values. This means that you can't write a statement where Dart expects a value. For example, you could use a conditional expression but not an if statement. In the previous example, _nobleGases[atomicNumber] != null; returns a boolean value. The function then returns a boolean value that indicates whether the atomicNumber falls into the noble gas range.

To create a function using shorthand notation, update the FunctionType in the builder. Use the #type(delegation: FunctionType) method provided by the builder. Set the type to FunctionType.SHORTEN, and the API will generate it as a shorthand method. Be aware that this function type requires content for the function itself. The API raises an exception when the body has no content.

Async functions:

When your use case requires an async function, the API provides an easy solution. The builder implementation includes an #async(boolean) function. Set this to true, and the library will generate a function that matches the Dart definition of an async function. If your function doesn't return a Future<void>, remember to update the return type using the #returnType(object) method.

Parameters:

Dart allows functions to have a varying number of parameters, but it is important to note that the language has different types of parameters, and not all can be combined. This might be familiar if you already know the language, but it is worth mentioning.

When defining a function with parameters, you can have required positional parameters, which can be followed by either named parameters or optional positional parameters. The use of both named and optional positional parameters together is not permitted. For more information, visit the Dart wiki or the parameters page.

To simplify the use of this library, you don't need to add parameters to a function through different methods. The project handles this process and performs necessary checks to ensure everything is correct.

You can add a parameter using different methods, each with its own syntax. Generally, use the #parameter(object) or #parameters(array) methods for this purpose.

Last updated