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

Current zig structs are "abstract" and maybe they shouldn't be. #2478

Closed
ghost opened this issue May 12, 2019 · 2 comments
Closed

Current zig structs are "abstract" and maybe they shouldn't be. #2478

ghost opened this issue May 12, 2019 · 2 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@ghost
Copy link

ghost commented May 12, 2019

Take a struct SomeStruct that contain a function "prototype" {x : fn(f32) f32,}

Next, imagine two functions: "fn afunc(x : f32) f32{..}" and "fn bfunc(x : f32) f32 {..}"

Assume the behavior of afunc and bfunc are completely different despite their equal parameters and return types.

Later, SomeStruct can be instantiated with either "afunc" or "bfunc" in place of x.

Problem: This goes against the ideal of explicitness in zig. In one aspect, zig structs (at version 0.4) have similarities with abstract classes in java.

Proposed alternative: Introduce interfaces into the zig language where the "abstract functions" feature can be moved. Then zig can be kept more explicit with "interface-implementing-structs" and normal types being visually distinct when referenced, e.g with surrounding angle brackets.

fn normalstruct(param : RandomStream) Output { ... }
fn ifaceImplementation(param : <IRandomStream>) Output { ... } 

Related: #1268, #1669
Mockup of alternative with zig interfaces: https://gist.github.com/user00e00/85f106624557b718673d51a8458dbab8

@tgschultz
Copy link
Contributor

Another alternative would be to make function definitions expressions (#1717) and use some variation of distinct types (#1595) to do something like this:

const IFoo = @distinct(fn(f32)f32);
pub const SomeStruct = struct {
    x: IFoo,
};

pub const aFunc: IFoo = fn(x: f32) f32 { ... };
pub const bFunc = fn(x: f32) f32 { ... };

const my_instance = SomeStruct{ .x = aFunc, }; //works
const my_other = SomeStruct{ .x = bFunc, }; //Compile error, expected type IFoo, got fn(f32)f32.

@andrewrk andrewrk modified the milestones: 1.0.0, 0.6.0 May 12, 2019
@andrewrk
Copy link
Member

andrewrk commented Jan 5, 2020

Since #1717 is accepted, I think it is safe to say that the open proposal #1595 supercedes this one.

@andrewrk andrewrk closed this as completed Jan 5, 2020
@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

2 participants