Skip to content

Recursive Destructuring

Andrew Matthews edited this page Aug 14, 2025 · 2 revisions

Recursive Destructuring

This shows fifthlang’s recursive destructuring in function parameters using a simple calculate_bmi example. This capability (destructuring directly in parameter lists with nested property bindings) is not currently available in C#.

calculate_bmi with nested property destructuring

// Destructure Person -> Vitals -> Height/Weight right in the parameter list
calculate_bmi(p: Person {
    name: Name,
    vitals: Vitals{
        age: Age,
        height: Height,
        weight: Weight
        }
    }) : float {
    return weight / (height * height);
}
  • The parameter pattern unwraps Person into local variables name, height, and weight
  • vitals is destructured recursively to access nested properties
  • No boilerplate reads like p.Vitals.Height; the signature binds exactly what the function needs

Minimal type sketch (for context)

class Person {
  Name: string;
  Vitals: Vitals;
}

class Vitals {
  Height: float;
  Weight: float;
}

Call site stays simple

main(): float {
  // assume p: Person is available
  return calculate_bmi(p);
}

Why it’s notable vs C#

  • C# supports deconstruction and rich pattern matching, but not destructuring directly in function parameter lists
  • fifthlang lets you bind nested properties at the boundary (the function signature) so your body stays minimal and intention-revealing

Notes

  • Names and nesting in the pattern must match the input type
  • This feature is under active development; see tests around recursive destructuring for the most current behavior

Clone this wiki locally