Are methods more than sugar? #15762
Replies: 6 comments 20 replies
-
Because similar to class methods in OOP, V's methods are tied to the type. Yes, of course you could use regular functions, but for example if you had a method that applied to strings, and a same-named method that applied to ints, to use regular functions instead you would either have to have a generic function that figured out which type it was called with, or you would have to have 2 differently named functions to handle the different types. It really is just a different way of doing things, but it can lead to much cleaner syntax. |
Beta Was this translation helpful? Give feedback.
-
I'm still wondering about this. 🤔
Are they? According to the manual:
And based on what you said here:
It looks to me as though V uses the type as part of the method's identity - but not really for anything else. But that seems unnecessary? I mean, V is statically typed, so the type is attached to the symbol - it has the information it needs to resolve the call, doesn't it? Let's take a practical example of potentially ambiguous methods: nums := [1, 2, 3]
println(nums.reverse()) // [3,2,1]
chars := "123"
println(chars.reverse()) // 321 What would be the problem with allowing the following? fn my_reverse(a []int) []int {
return a.reverse()
}
fn my_reverse(s string) string {
return s.reverse()
}
nums := [1, 2, 3]
println(nums.my_reverse()) // [3,2,1]
println(my_reverse(nums)) // [3,2,1]
chars := "123"
println(chars.my_reverse()) // 321
println(my_reverse(chars)) // 321 If this looks like overloading, it isn't - it's a semantic change, in which the first argument of any function becomes a part of the function identity, just as it does with the current function declaration syntax. There's no ambiguity and no guesswork involved for the compiler here - this semantic change would make declarations like In terms of call semantics, "method calls" and "function calls" in V are both just function calls, right? As per the manual, "a method is a function with a special receiver argument". That's it, the call semantics are no different, I think? The difference is how the functions are looked up at compile-time - method calls use the type and the function name, whereas function calls use only the function name. The type + name identity approach is actually stronger. Why not apply it everywhere? |
Beta Was this translation helpful? Give feedback.
-
Well first g++ translated class into struct and method into functions so in the end every class is syntax sugar, in theorical it would be working equal, but in practical it would be adition work to compiler pass argument..., and it would be litle bit confusing fir programator now I can play to OOP and everybody from OOP will understand and they will be thinking that is OOP without knoledge backend and the last think in future will be able allow (in theoreticaly) play with visibility ... |
Beta Was this translation helpful? Give feedback.
-
The advantage of how V does it, is also the clarity and readability it provides. The method is clearly assigned to a struct, even if the name of the methods are the same. If the person doesn't want to assign methods to structs, but wants parametric polymorphism, sum types and generics can be used with functions in V. |
Beta Was this translation helpful? Give feedback.
-
OOP Familiarity: yes, I don't think you lose that by allowing even more functions to be called as methods? And not everyone is looking for OOP - the qualities of functional programming definitely has more mind share today than it did even a few years ago. Unifying functions and methods would cater to both camps. Clarity and readability: I think this argument is debatable. When you call a method defined in some other file, it's not immediately obvious where that method comes from or why it's callable. Explicit imports are arguably more ceremonious, but generally improve clarity and readability: if you're looking at just the one file, you're going to see, at the top of the file, where any functions come from. With good IDE support, adding import statements is easy, and the resulting source files can be read and understood more independently. For what it's worth, Nim has this feature and this decision has worked out very well for the language - it has some well-known minor limitations, but (as argued) I think having to explicitly import your dependencies is "a good thing" for readability. You might should take a look at Nim's codebase before dismissing the idea - in my opinion, it's very elegant, explicit, and readable* 🙂 * if you can see past the rest of Nim's syntax to this particular feature - arguably, much of the rest of the language isn't simple and looks more like a "systems programming language" than an "application language"... |
Beta Was this translation helpful? Give feedback.
-
This would also require function overloading, which is a terrible idea. It slows down the compiler a lot, complicates the language, makes interop with other languages (primarily C) almost impossible, and makes large code bases much less readable. V and Go have been designed to have no function overloading. |
Beta Was this translation helpful? Give feedback.
-
I was wondering, is there any real difference between methods and functions in V?
It doesn't look like V really has objects or method tables or anything like that? By the looks of it, methods are just functions with a different call syntax?
If so, why are methods even a thing?
Wouldn't it make more sense to just allow any function to be called using method call syntax for the first argument?
Then it would be just a choice if you use one syntax or the other - the functions would all just be functions with no special syntax in the declaration, and no constraints on the call syntax.
Is there a reason for methods beyond syntax?
Beta Was this translation helpful? Give feedback.
All reactions