0Pricing
Zig Academy · Lesson

anytype Parameters

Accept any argument with duck typing.

anytype Parameters is a free Zig Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Accept Anything with anytype

Sometimes you do not want to name the type at all. Declare a parameter as anytype and Zig infers its type from the argument. 🦆

fn show(x: anytype) void {}

Type Is Inferred at the Call

With anytype there is no leading type parameter. Each caller's argument decides the concrete type for that specialization.

show(42);
show(true);

This Is Duck Typing

Zig only checks that the body's operations work for the argument you pass. If it has the right shape, it fits. That is duck typing.

Recover the Type If You Need It

Inside the function you can still ask for the type using @TypeOf, then use it for locals or return values.

fn dup(x: anytype) @TypeOf(x) {
    return x;
}

Great for Print-Style Helpers

Functions that work on many shapes love anytype. Zig's own print accepts an anytype tuple of arguments to format.

std.debug.print("{any}\n", .{x});

Constraints Come from the Body

There is no explicit interface. If you call x.len inside, then only arguments that actually have a len field will compile.

fn size(x: anytype) usize {
    return x.len;
}

Errors Are Per-Specialization

If a caller passes a type the body cannot handle, Zig reports the error at that call site, naming the exact type that failed.

Validate Inputs with comptime

For clearer errors you can inspect the type early with @typeInfo and reject the wrong shape using a compile-time check.

anytype vs comptime T: type

Use comptime T: type when you must name the type up front; reach for anytype when inference and flexibility matter more.

Still Zero Run-Time Cost

Like every Zig generic, an anytype function is specialized at compile time, so the inferred flexibility costs nothing when the program runs.

Mix anytype with Named Types

A function can blend both styles: one anytype parameter for flexible input alongside ordinary typed parameters for fixed data.

fn join(sep: u8, x: anytype) void {
    _ = sep;
    _ = x;
}

Quick Check

You write fn show(x: anytype). How does Zig decide the concrete type of x?

Recap

The anytype keyword lets a parameter accept any argument, with the type inferred and the body checked per call. It is Zig's flexible duck typing. 🎯

Frequently asked questions

Is the “anytype Parameters” lesson free?

Yes — the full text of “anytype Parameters” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “anytype Parameters”?

Accept any argument with duck typing. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “anytype Parameters” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Functions That Take a Type
  2. Generic Data Structures
  3. @TypeOf and Type Reflection
  4. anytype Parameters
← Back to Zig Academy