vlang

Does the V language allow to extend base types?


In V language methods are defined separately from the data structures.

Does the V language allow to define methods on base types, like Array?

Is it possible to write my_method method like

fn (array Array) my_method() { ... }

list := ["a", "b"]
list.my_method()

Solution

  • Only in your own modules were you control the implementation - and I don't think there's any plans to support it.

    You might do something like it with either generics or struct embeds, when they're fully done. But I doubt there'll be support for it for builtin module types.

    Edit: (In the future it might also be possible to do something with Sum types)