type alias, design flaw or design pattern? #18556
-
Hi, I'm testing type alias by the following code
I have 2 questions.
I'm not sure if this is a design flaw (bad) or design pattern (good), if it's a design pattern, then this will be a way to add new methods to existing types, but it's a little bit redundant to use type alias to do it in my point of view. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
No, it's not a flaw. If you were allowed to define new methods on built-in types, it could lead to confusion in the code base, bug reports because someone saw your method without seeing your definition, and wondering why it didn't work in their code, etc., etc. Creating an alias is essentially creating a new type, and new types can have whatever methods you wish to define on them, as well as the ones they inherit from their parent types. This is less confusing in V due to the naming conventions. If someone sees a method on a name with a capital letter, they know it's something defined in your code (or something you imported), rather than a built-in type. |
Beta Was this translation helpful? Give feedback.
-
then is it some kind of "inheritance"? |
Beta Was this translation helpful? Give feedback.
No, it's not a flaw. If you were allowed to define new methods on built-in types, it could lead to confusion in the code base, bug reports because someone saw your method without seeing your definition, and wondering why it didn't work in their code, etc., etc.
Creating an alias is essentially creating a new type, and new types can have whatever methods you wish to define on them, as well as the ones they inherit from their parent types.
This is less confusing in V due to the naming conventions. If someone sees a method on a name with a capital letter, they know it's something defined in your code (or something you imported), rather than a built-in type.