I want fn's for self: *const MyType and self: *MyType

If I have:
const my_x: *const MYX = myx.init();
then I must also have:
fn deinit(self: *const MYX) void {.,.}

Wouldn’t it be safer to have varconst which represents both var and const, to be used only in the types methods? example:
fn deinit(self: *varconst MYX) void {.,.}

You can write a method that accepts both like this:

const MYX = struct {
      fn deinit(self: anytype) void {

You also can switch on the type of self to adapt the return type:

const MYX = struct {
      fn getSomething(self: anytype) switch(@TypeOf(self)) {
          *MYX => []Something,
          *const MYX => []const Something,
          else => undefined,
      } {

I am unsure what you mean by making things more safe.
Do you want *varconst to auto-magically change the resulting return type to var/const based on whatever self is?


Yes, that is a doable solution. It makes sense.

1 Like