Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Base Functionality #3

Open
2 of 9 tasks
nylvon opened this issue Nov 15, 2023 · 2 comments · Fixed by #5
Open
2 of 9 tasks

Implement Base Functionality #3

nylvon opened this issue Nov 15, 2023 · 2 comments · Fixed by #5
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request new core feature Adds a new core feature.

Comments

@nylvon
Copy link
Owner

nylvon commented Nov 15, 2023

Interfaces

An interface should:

  1. Be zero-cost.
  2. Be able to generate types based off of them.
  3. Be able to "combine" with other interfaces in various ways.
  4. Be able to be derived from types.
  5. Be able to check if types implement them at compile time.
  6. Be able to "grant" types a checkmark that lets other types know what interfaces this type implements without having to re-check.

Proposed example usage

  1. Creating interfaces
// A simple reader interface
const IReader = Interface.init([_]Field {
		Interface.Field("buffer", []u8),
		Interface.Declaration("read_fn", [_]type {[]u8, []u8}, []u8)
	});

// "grant" returns the type defined below at compile-time with a special declaration that contains a list of interfaces that it has been checked against and passed the checks for. If any of them fail, compilation fails and an error message is shown detailing why the type does not implement any of the interfaces mentioned.
const custom_reader_type = Interface.Grant([1]Interface{IReader},
	.{
		buffer: []u8,

		pub fn custom_read_fn(path: []u8, options: []u8) []u8 {
			// ...
		}
	});
  1. Type merging, type generation, type granting, and interface merging.
// Naive type-merging
const writer_type = struct { ... };
const reader_type = struct { ... };

// Obtain a writer and reader combined type
const writer_reader_type = Interface.AsType([_]type {writer_type, reader_type});

// Safe type-merging
const safe_writer_type = Interface.Grant([1]Interface{IWriter}, .{ ... });
const safe_reader_type = Interface.Grant([1]Interface{IReader}, .{ ... });

// Merged type
const safe_writer_reader_type = Interface.AsType([_]type {safe_writer_type, safe_reader_type});
const safe_writer_reader_interface = Interface.Merge([_]Interface{IReader, IWriter});
const safe_writer_reader_type_from_interface = Interface.AsType([_]Interface {safe_writer_reader_interface});

// These should be identical.
try std.testing.expectEqual(safe_writer_reader_type == safe_writer_reader_type_from_interface);

Related:

@nylvon nylvon added documentation Improvements or additions to documentation enhancement New feature or request new core feature Adds a new core feature. labels Nov 15, 2023
@nylvon nylvon self-assigned this Nov 15, 2023
@nylvon
Copy link
Owner Author

nylvon commented Dec 10, 2023

The Rule type implementation has started with 4d37494.

@nylvon nylvon linked a pull request Dec 10, 2023 that will close this issue
4 tasks
@nylvon nylvon closed this as completed in #5 Jan 2, 2024
@nylvon nylvon reopened this Jan 2, 2024
@nylvon
Copy link
Owner Author

nylvon commented Jan 2, 2024

A new syntax is here, and is more fluent than the one initially proposed.
See the last comment on #5 for more details.

Here's an example (from #5) how it looks right now:

const IsVec3Simple =
    Blank()
        .And(IsInType(FindField("x")))
        .And(IsInType(FindField("y")))
        .And(IsInType(FindField("z")));

const IsVec3Alternative =
    Blank()
        .And(IsInType(FindField("buffer")));

const IsVec3Full =
    Blank()
        .Or(IsVec3Simple)
        .Or(IsVec3Alternative);

const IsVec3Accessors =
    Blank()
        .And(IsFunction(FindDeclaration("get_X")))
        .And(IsFunction(FindDeclaration("get_Y")))
        .And(IsFunction(FindDeclaration("get_Z")));

const IsVec3 = 
    Blank()
        .And(IsVec3Full)
        .And(IsVec3Accessors);

// ...

pub fn ComplexVec3Op(arg: anytype) !void {
    comptime try IsVec3.Check(arg);
    
    // ... do something with arg ...
    // ... knowing it's a vec3 ...

}

@nylvon nylvon changed the title Implement Interfaces Implement Base Functionality Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request new core feature Adds a new core feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant