• 1Installation
  • 2HelloWorld Function
  • 3Function with Arguments
  • 4Function with Variadic Arguments
  • 5Partial Application
  • 6Function Composition
  • 7Function Chaining
  • 8Extend R.apex

R.apex

  • Docs
  • Tutorials
Getting started with R.apex

10 min 30 sec

HelloWorld Function

Apex does not support first class functions, and we have NO WAY to get around it. However, we can create an invocable object camouflaged as a function, and it is referred to as a Func. Or more precisely, it is an instance of class Func. In R.apex, we roughly refer to instances of Func when we mention functions, to make things clear.

Here is how we create a function that returns Hello World.

public class HelloWorldFunc extends Func {
    public HelloWorldFunc() {
        super(0);
    }

    public override Object exec() {
        return 'Hello World';
    }
}
​x
 
1
public class HelloWorldFunc extends Func {
2
    public HelloWorldFunc() {
3
        super(0);
4
    }
5
​
6
    public override Object exec() {
7
        return 'Hello World';
8
    }
9
}

And then we get a function!

Func helloworld = new HelloWorldFunc();
1
 
1
Func helloworld = new HelloWorldFunc();

Let's invoke this function.

String message = (String)helloworld.run();
System.debug(message);
// Hello World
3
 
1
String message = (String)helloworld.run();
2
System.debug(message);
3
// Hello World

That's how easy it is to create a function in R.apex.

Done
Copy