Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Type reflection, coercion, and generation written in Zig, for Zig.

License

NotificationsYou must be signed in to change notification settings

nylvon/metaplasia

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

metaplasia_logo

What is this?

Metaplasia is a library that offers users the ability to do a fewfun things with their types and objects in Zig, such as:

  1. Easy reflection
  2. Interfaces
    • Compile-time and run-time (and hybrid)!
  3. Type merging
  4. Type generation

Why would I want any of this?

  • Interfaces are important and make sure that your code obeys a standard implementation such that it can be swapped out with another bit without needing to re-write everything, thus increasing modularity!But they also serve an important role as sanity checks for your code!
// A simple reader interfaceconstIReader=Blank().And(IsInType(FindFieldAs("buffer", []u8)).And(IsInType(FindFunctionAs("read_fn",fn([]u8, []u8) []u8));// "grant" returns the type defined below at compile-time if and only if it implements IReaderconstcustom_reader_type=IReader.Grant(struct {buffer: []u8,pubfnread_fn(path: []u8,options: []u8) []u8 {// ...}});
  • Ever wanted to have a reader and a writer in one package, having those written already, but did not want to write this mixed type by hand? Type merging is the solution! Simply pass two types as
// Naive type-mergingconstwriter_type=struct {... };constreader_type=struct {... };// Obtain a writer and reader combined typeconstwriter_reader_type=InferAndMerge([_]type {writer_type,reader_type});// Safe type-mergingconstsafe_writer_type=IWriter.Grant(struct {... });constsafe_reader_type=IReader.Grant(struct {... });// This type matches both interfaces, because the original types matched them, if not, we'd get a compile error explaining the situation!// This type can be used as both a writer and a reader, swap freely!constsafe_writer_reader_type=InferAndMerge([_]type {safe_writer_type,safe_reader_type});
  • Now think of a game, where you would have a lot of types, and you would want to re-use them to build more advanced, complex types based off of the original ones. Or, what about procedural types?
// Some base type that's going to be used in a lot of placesconstentity_type=InferAndMerge([_]type {health_type,damage_type,identity_type,...});// A new player type that 'extends' the entity type. Inheritance, but without all the fuss!constplayer_type=InferAndMerge([_]type {entity_type,physics_type,chat_type,... });// ...// insert example for procedural types};

Current status

Below is the status of current features.Blank means not implemented.

  • Reflection (#2)
  • Interfaces (#5)
  • Type merging
  • Type generation

[8]ページ先頭

©2009-2025 Movatter.jp