zls icon indicating copy to clipboard operation
zls copied to clipboard

Information on hover for language constructs

Open SuperAuguste opened this issue 2 years ago • 1 comments

Thanks @slimsag for sharing this idea here!!

For example, If I hover on the |bruh| here:

if (thing) |bruh| { ... } else { ... }

a hover box should appear with something to the tune of

This is a capture named `bruh`; if `thing` is not null, `bruh` will be the unwrapped value of `thing`, otherwise the else branch if invoked.

SuperAuguste avatar May 06 '23 23:05 SuperAuguste

I while ago I experimented with this idea and I tried to explain some language constructs by showing how they would translate into other low-level languages (C, C++, Rust)

Optionals

C++

std::optional<T>

Actions on an optional opt Zig C++
checks whether opt contains a value opt != null opt.has_value() or (bool)opt
return the contained a value of opt opt.? opt.value() or *opt
return the contained a value of
opt if available, other otherwise
opt orelse other opt.value_or(other)

Rust

std::option<T>

Actions on an optional opt Zig Rust
checks whether opt contains a value opt != null opt.is_some()
return the contained a value of opt opt.? opt.unwrap()
return the contained a value of
opt if available, other otherwise
opt orelse other opt.unwrap_or(other)

Tagged Union

Zig

const U = union(enum) {
    foo: Foo,
    bar: Bar,
    baz: Baz,
};

C

Unlike this C representation, tagged unions in Zig don't have well-defined memory layout.

struct U {
    enum Tag : unsigned _BitInt(2) {
        foo,
        bar,
        baz,
    } tag;
    union
    {
        Foo foo;
        Bar bar;
        Baz baz;
    } data;
};
Actions on a tagged union u Zig C
return the current tag std.meta.activeTag(u) u.tag
return the tag type std.meta.Tag(U) U::TagType
return whether u holds Foo u == .foo u.tag == U::TagType::foo
return the value Foo from u u.foo u.data.foo
do something based on the current tag in u switch (u) {...} switch (u.tag) {...}

C++

std::variant<Foo, Bar, Baz>

Actions on a tagged union u Zig C++
return whether u holds Foo u == .foo std::holds_alternative<Foo>(u)
return the value Foo from u u.foo std::get<Foo>(u) or std::get<0>(u)
do something based on the current tag in u switch (u) {...} if-else chain or std::visit

Rust

enum E { foo(Foo), bar(Bar), baz(Baz) }

Slices

Zig

const S = []T;

C

Unlike this C representation, slices in Zig don't have well-defined memory layout.

struct S {
    T* ptr;
    size_t len;
};

C++

std::span<T> or std::string_view if T is u8 i.e. char

Actions on a slice s Zig C++
return the pointer to the start of the slice s.ptr s.data() or std::data(s)
return the number of elements s.len s.size() or std::size(s)
return the element at the index i s[i] s[i]
iterate over all elements as const for (s) |elem| {...} for (const T& elem : s) {...}
iterate over all elements for (s) |*elem| {...} for (T& elem : s) {...}

Rust

[T] or str if T is u8

Actions on a slice s Zig Rust
return the number of elements s.len s.len()
return the element at the index i s[i] s.get_unchecked(i)
iterate over all elements as const for (s) |elem| {...} for elem in s {...} where s is &[T]
iterate over all elements for (s) |*elem| {...} for elem in s {...} where s is &mut [T]

Techatrix avatar Oct 11 '23 21:10 Techatrix