#Zimpl: Zig interfaces

1 messages · Page 1 of 1 (latest)

eternal mauve
#

A simple implementation of Rust style type traits for Zig generics: https://github.com/permutationlock/ztrait

When I first saw @typeInfo and comptime Zig, I started wondering whether it was possible to implement type classes/traits. I decided to give it a try a couple of days ago and it was surprisingly easy to get something working.

This only does trait verification and does not restrict how types are used (you can use declarations that don't appear in any required traits).

Edit: I don't think that anyone should use Ztrait for any practical purpose. Check out Zimpl for my next attempt at constrained generics: https://github.com/permutationlock/ztrait

GitHub

A simple version of Rust style type traits in Zig. Contribute to permutationlock/ztrait development by creating an account on GitHub.

static igloo
#

That's quite interesting.

modest leaf
#

Having this is nice: ```rs
const MyCounter = struct {
comptime { trait.implements(Incrementable).assert(@This()); }
pub const Count = i32;

#

Good self-enforcement when building up decls on a big project

eternal mauve
#

Awesome! For some reason I thought it wouldn't be possible to assert you were implementing a trait, but that's so easy 🙂

bright raven
#

comtime power! awesome project.zigpill

modest leaf
vocal cradle
#

also refAllDeclsRecursive if you need something more powerful

lost dawn
#

Nice, this wraps up a ton of one off comptime boilerplate work people have been doing 👏 you could easily grow this into a comptime utils lib this is slick asf

eternal mauve
#

The library has gone through a lot of changes, though its core functionality is roughly the same, and I believe that it is in an interesting spot. It also now supports the package manager.

I wrote a little article discussing Zig's anytype which has some examples using the ztrait library: https://musing.permutationlock.com/posts/blog-working_with_anytype.html

None of what I've made is prescriptive: I've had a lot of fun working on the project, but I don't know if anyone should really be using generics this way.

median crescent
#

i think of it as a "did i fuck up the implementation" checker

#

like, sure you'll find out once you try to use it somewhere. but why not when it's being written already.

#

could be useful for people who write libraries too. a self consistency checker for the outgoing interface surfaces.

#

is there something for checking if anytype field accesses in a function satisfy atleast one (specific or not) struct's fields?

eternal mauve
# median crescent is there something for checking if ``anytype`` field accesses in a function sati...

I'm not sure exactly what you have in mind, but the library started from the point of view of Rust traits. Thus it only looks at declarations and assumes that functions won't directly be accessing the fields of generic parameters.

Of course, beyond convention, there isn't really anything wrong with accessing fields. It would certainly possible to define things in such a away that structs are checked for the existence and type signature of their fields instead (or in addition to decls).

eternal mauve
south urchin
#

This is awesome, great work!

eternal mauve
#

I put out a v1.0 "release" of Zimpl that boils down to a 30 line file with one comptime function. A proof-of-concept Zimpl version of std.io is in the examples. Now I think I'm going back to using concrete types and actually being productive in my free time...

twin anchor
#

does this use vtable?

eternal mauve
#

Zimpl: Zig interfaces

elder garden
#

in your read_file.zig example, I see try io.streamUntilDelimiter(file, .{}, &fbs, .{}, '\n', 32);

#

.{} is the interface struct?

eternal mauve
# elder garden `.{}` is the interface struct?

Yes. E.g. the first .{} is default constructing Impl(io.Reader, @TypeOf(file)) which is the io.Reader(std.fs.File) struct with the default values for the read and ReadError fields set equal to std.fs.File.read and std.fs.File.ReadError.

stark obsidian
#

This is awesome!

I've wanted an interface-like system for Zig for a while, this is just what I wanted.

hollow mulch
#

Nice project! I myself wrote a project a while back for functionality like this. Cool to see the parallels. Carcinization crabisrave indeed is not limited to Sealife.