Skip to content

data classes

tekdemo edited this page Oct 4, 2024 · 1 revision

Data Classes

Data classes are a compound data types, often called structs in other languages, are a convenient way to bundle multiple named data types together.

public TargetData{
    double distance;
    double height;
    double relativeAngle;
}

These data types can be treated just like any other class, and returned from functions.

public TargetData getTargetInfo(){
    var data = new TargetData();
    data.distance = //get data somehow
    data.height = //get data somehow
    data.relativeAngle = //get data somehow
    return data;
}

When combined with optionals, you can make very simple to use APIs.

Data classes can be very useful to provide more "atomic" data, without worry that one underlying part of your data type might change. This is often the case with sensors, which might subtly change while you're calculating data based on it.

They're also useful for correlating multiple data types between functions. The WPILib uses this in several places, such as Swerve's ModuleState (angle and distance),

Clone this wiki locally