Current zig structs are "abstract" and maybe they shouldn't be. #2478
Labels
proposal
This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone
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.
Related: #1268, #1669
Mockup of alternative with zig interfaces: https://gist.github.com/user00e00/85f106624557b718673d51a8458dbab8
The text was updated successfully, but these errors were encountered: