Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ray project Ray RayFunc Interfaces

From Leeroopedia
Knowledge Sources
Domains Distributed_Computing, Functional_Programming
Last Updated 2026-02-13 17:00 GMT

Overview

Concrete tool for defining type-safe remote function references provided by the Ray Java SDK.

Description

The RayFunc interface hierarchy provides a family of serializable functional interfaces for declaring remote functions. RayFunc is the root marker interface extending Serializable. RayFuncR<R> marks functions with a return value. The concrete interfaces RayFunc0<R> through RayFunc6<T0,...,T5,R> handle functions with 0 to 6 parameters. Void variants (RayFuncVoid0 through RayFuncVoid6) handle side-effect-only functions.

All interfaces are auto-generated and should not be modified manually.

Usage

Use these interfaces implicitly via Java method references when calling Ray.task(). You do not need to implement these interfaces directly — Java method references automatically match the correct functional interface based on parameter count.

Code Reference

Source Location

  • Repository: ray-project/ray
  • File: java/api/src/main/java/io/ray/api/function/RayFunc.java (root interface)
  • File: java/api/src/main/java/io/ray/api/function/RayFunc0.java through RayFunc6.java

Signature

// Root marker interface
public interface RayFunc extends Serializable {}

// Return-value marker
public interface RayFuncR<R> extends RayFunc {}

// 0-parameter function
@FunctionalInterface
public interface RayFunc0<R> extends RayFuncR<R> {
    R apply() throws Exception;
}

// 1-parameter function
@FunctionalInterface
public interface RayFunc1<T0, R> extends RayFuncR<R> {
    R apply(T0 t0) throws Exception;
}

// ... up to RayFunc6<T0, T1, T2, T3, T4, T5, R>

Import

import io.ray.api.function.RayFunc;
import io.ray.api.function.RayFunc0;
import io.ray.api.function.RayFunc1;
// etc.

I/O Contract

Inputs

Name Type Required Description
Method reference Lambda/MethodRef Yes A Java method reference or lambda matching the functional interface signature (e.g., MyClass::myMethod)

Outputs

Name Type Description
Function reference RayFuncR<R> A serializable function descriptor that can be passed to Ray.task() or Ray.actor()

Usage Examples

Defining Remote Functions via Method References

import io.ray.api.Ray;
import io.ray.api.ObjectRef;

public class RemoteFunctions {
    // 0-parameter function (matches RayFunc0<Integer>)
    public static int getAnswer() {
        return 42;
    }

    // 1-parameter function (matches RayFunc1<Integer, Integer>)
    public static int square(int x) {
        return x * x;
    }

    // 2-parameter function (matches RayFunc2<Integer, Integer, Integer>)
    public static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        Ray.init();

        // Method references are automatically matched to RayFunc interfaces
        ObjectRef<Integer> r1 = Ray.task(RemoteFunctions::getAnswer).remote();
        ObjectRef<Integer> r2 = Ray.task(RemoteFunctions::square, 5).remote();
        ObjectRef<Integer> r3 = Ray.task(RemoteFunctions::add, 3, 4).remote();

        System.out.println(r1.get()); // 42
        System.out.println(r2.get()); // 25
        System.out.println(r3.get()); // 7

        Ray.shutdown();
    }
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment