# What is the different between @This() and struct?

**URL:** <https://ziggit.dev/t/what-is-the-different-between-this-and-struct/17886>\
**Category:** Explain\
**Created:** [October 8, 2026, 2:51am UTC](https://ziggit.dev/t/what-is-the-different-between-this-and-struct/17886 "2026-10-08T02:51:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wauuuu](https://ziggit.dev/letter_avatar_proxy/v4/letter/w/5fc32e/32.png) [@Wauuuu](https://ziggit.dev/u/Wauuuu)\
**Post date:** [October 8, 2026, 2:51am UTC](https://ziggit.dev/t/what-is-the-different-between-this-and-struct/17886/1 "2026-10-08T02:51:49Z")

</div>

Hello, guys. So I starting to learn and read zig codebase in github/codeberg. I notice some of the projects/codebase use @This() with PascalCase.zig filenames and some of them are using struct with lowercase.zig filenames. Is there any difference using @This() and struct?

Sorry, if some of you don’t understand my questions. My English is not good and I’m still learning programming. Thanks.

---

<div class="post-metadata">

**Author:** ![biom4st3r](https://ziggit.dev/user_avatar/ziggit.dev/biom4st3r/32/4342_2.png) [@biom4st3r](https://ziggit.dev/u/biom4st3r)\
**Post date:** [October 8, 2026, 3:07am UTC](https://ziggit.dev/t/what-is-the-different-between-this-and-struct/17886/2 "2026-10-08T03:07:36Z")

</div>

`@This()` refers to the inner most type

```zig
const T = union{
   comptime{
      std.debug.expect(T == @This());
   }
}

```

All files are implicitly structs, so in the base of a file the inner most type would be a struct

---

<div class="post-metadata">

**Author:** ![Wauuuu](https://ziggit.dev/letter_avatar_proxy/v4/letter/w/5fc32e/32.png) [@Wauuuu](https://ziggit.dev/u/Wauuuu)\
**Post date:** [October 8, 2026, 3:32am UTC](https://ziggit.dev/t/what-is-the-different-between-this-and-struct/17886/3 "2026-10-08T03:32:09Z")

</div>

So, as far as I understand, when we use `@This()` inside a `struct`, `union`, etc. It refers to the type/container itself, right? In this case, `const T = union { ... }`, so `@This()` refers to that anonymous union.

But what happens when `@This()` is used outside an explicitly declared `struct`, `union`, etc?

For example, I came across this code in `zigtools/zls` (`DiagnosticsCollections.zig`):

```zig
//! Collects various sources of error messages to be sent out as LSP diagnostics.

const std = @import("std");
const lsp = @import("lsp");
const tracy = @import("tracy");
const offsets = @import("offsets.zig");
const Uri = @import("Uri.zig");

io: std.Io,
allocator: std.mem.Allocator,
mutex: std.Io.Mutex = .init,
tag_set: std.array_hash_map.Auto(Tag, struct {
    version: u32 = 0,
    error_bundle_src_base_path: ?[]const u8 = null,
    error_bundle: std.zig.ErrorBundle = .empty,
    diagnostics_set: Uri.ArrayHashMap(struct {
        arena: std.heap.ArenaAllocator.State = .{},
        diagnostics: []lsp.types.Diagnostic = &.{},
        error_bundle: std.zig.ErrorBundle = .empty,
    }) = .empty,
}) = .empty,
outdated_files: Uri.ArrayHashMap(void) = .empty,
transport: ?*lsp.Transport = null,
offset_encoding: offsets.Encoding = .@"utf-16",

const DiagnosticsCollection = @This();

// The rest of the code...

```

Here, there is no explicit `struct DiagnosticsCollection { ... }` surrounding these fields. So what exactly does `@This()` refer to in this case?

Is the file itself implicitly acting as the struct/container? And if so, why do we define:

```zig
const DiagnosticsCollection = @This();

```

instead of explicitly wrapping everything in a struct like:

```zig
const DiagnosticsCollection = struct {
    io: std.Io,
    allocator: std.mem.Allocator,
    // ...
};

```

I’m trying to understand what is happening under the hood here and why this pattern is used in Zig.

---

<div class="post-metadata">

**Author:** ![alanza](https://ziggit.dev/user_avatar/ziggit.dev/alanza/32/6006_2.png) [@alanza](https://ziggit.dev/u/alanza)\
**Post date:** [October 8, 2026, 4:01am UTC](https://ziggit.dev/t/what-is-the-different-between-this-and-struct/17886/4 "2026-10-08T04:01:33Z")

</div>

When `@This()` occurs outside any other container, it refers to the type of the file itself (which is a struct type). Files are always namespaces = containers = struct types.

For files which are instantiable types, like `std.Io`, this `@This()` pattern is necessary in order to write methods on that type.

---

<div class="post-metadata">

**Author:** ![biom4st3r](https://ziggit.dev/user_avatar/ziggit.dev/biom4st3r/32/4342_2.png) [@biom4st3r](https://ziggit.dev/u/biom4st3r)\
**Post date:** [October 8, 2026, 4:09am UTC](https://ziggit.dev/t/what-is-the-different-between-this-and-struct/17886/5 "2026-10-08T04:09:16Z")

</div>

Zig implicitly treats all files as if they are structs

```zig
struct {
    //! Collects various sources of error messages to be sent out as LSP diagnostics.
    
    const std = @import("std");
    const lsp = @import("lsp");
    const tracy = @import("tracy");
    const offsets = @import("offsets.zig");
    const Uri = @import("Uri.zig");
    
    io: std.Io,
    allocator: std.mem.Allocator,
    mutex: std.Io.Mutex = .init,
    tag_set: std.array_hash_map.Auto(Tag, struct {
        version: u32 = 0,
        error_bundle_src_base_path: ?[]const u8 = null,
        error_bundle: std.zig.ErrorBundle = .empty,
        diagnostics_set: Uri.ArrayHashMap(struct {
            arena: std.heap.ArenaAllocator.State = .{},
            diagnostics: []lsp.types.Diagnostic = &.{},
            error_bundle: std.zig.ErrorBundle = .empty,
        }) = .empty,
    }) = .empty,
    outdated_files: Uri.ArrayHashMap(void) = .empty,
    transport: ?*lsp.Transport = null,
    offset_encoding: offsets.Encoding = .@"utf-16",
    
    const DiagnosticsCollection = @This();
    
    // The rest of the code...
}

```

---

<div class="post-metadata">

**Author:** ![Wauuuu](https://ziggit.dev/letter_avatar_proxy/v4/letter/w/5fc32e/32.png) [@Wauuuu](https://ziggit.dev/u/Wauuuu)\
**Post date:** [October 8, 2026, 12:10pm UTC](https://ziggit.dev/t/what-is-the-different-between-this-and-struct/17886/6 "2026-10-08T12:10:06Z")

</div>

Ahh I see, so there’s no difference on performance side? It just preference how the author’s want their codebase to design?
