For the matter of consistency, you may decide to drop int str() method in favor of explicit type conversion.
V version: 0.1.19
OS: : MacOS 10.14
What did you do?
According to documentation:
To force a different type, use type conversion: the expression T(v) converts the value v to the type T
mut n := 1
n_str = string(n)
What did you expect to see?
Successful type conversion
What did you see instead?
cannot cast int to string, use str() method instead
T(v) is casting, int.str() is not casting, but running an entire subprogram to convert the string.
From a user perspective, it seems inconsistent to me if some types can be casted and some can not, irregardless of internal implementation.
T(v) is casting, int.str() is not casting, but running an entire subprogram to convert the string.
If we want to retain this (rather disputable) difference, then there should be crystal clear rules in V for library makers how to construct APIs.
Semantically I regard casting as a default conversion between arbitrary types without thinking about compiler internals (it's the same as with intrinsics - I don't care that compiler compiled them to some specific code, because I do care just about the programming interface).
In general my very own rules for all type conversions are:
int -> str can offer some printf-like formatting with padding etc. with reasonable performance which is still higher than calling successively several methods; it's also more readable)T(v) is casting, int.str() is not casting, but running an entire subprogram to convert the string.
So what is the advantage to have both in place when the result is identical? That seems to go again the V-rule "do same stuff the same way"
There's only int.str() @gslicer
It works just like in Go. string(int) is a confusing cast that doesn't do what it's expected to do, and they are considering removing it:
Good comment on that issue:
I would not expect an innocuously looking conversion to invoke an expensive number formatting routine.
Most helpful comment
T(v) is casting, int.str() is not casting, but running an entire subprogram to convert the string.