v icon indicating copy to clipboard operation
v copied to clipboard

.str() method inconsistent behaivor

Open kitech opened this issue 1 month ago • 1 comments

Describe the bug

Code: https://play.vlang.io/p/3c2efc886d


interface Strable {
	str() string
}

fn need_string(a Strable) {}

struct Foo {}

x := Foo{}

println(x.str())  // V think there is a x.str() method
need_string(x)  // V think there is not a x.str() method

Reproduction Steps

Click run button on play.

Expected Behavior

I think V should have conisistent behaivor for .str()

Maybe the .str() in println(x.str()) is compiler magic that I'm not sure.

Current Behavior

Output:

code.v:13:13: error: `Foo` doesn't implement method `str` of interface `Strable`
   11 | 
   12 | println(x.str())  // V think there is a x.str() method
   13 | need_string(x)  // V think there is not a x.str() method
      |             ^
Exited with error status 1

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.12 d0dc13ee63884aa7a573dac5b12f3fbd0c30bf62

Environment details (OS name and version, etc.)

|V full version      |V 0.4.12 d0dc13ee63884aa7a573dac5b12f3fbd0c30bf62
|:-------------------|:-------------------
|OS                  |linux, Debian GNU/Linux 12 (bookworm) (VM)
|Processor           |2 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz
|Memory              |0.56GB/2.02GB
|                    |
|V executable        |/home/admin/v/v
|V last modified time|2025-11-25 00:25:31
|                    |
|V home dir          |OK, value: /home/admin/v
|VMODULES            |OK, value: .vmodules
|VTMP                |OK, value: /tmp/v_0
|Current working dir |OK, value: /home/admin/playground
|                    |
|Git version         |git version 2.39.5
|V git status        |N/A
|.git/config present |true
|                    |
|cc version          |cc (Debian 12.2.0-14+deb12u1) 12.2.0
|gcc version         |gcc (Debian 12.2.0-14+deb12u1) 12.2.0
|clang version       |Debian clang version 14.0.6
|tcc version         |tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
|tcc git status      |Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc
 Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc

|emcc version        |N/A
|glibc version       |ldd (Debian GLIBC 2.36-9+deb12u10) 2.36

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

kitech avatar Nov 25 '25 01:11 kitech

Yes, it is compiler magic, and it is mostly handled in the C backend, at present.

We've been talking about this for years... that the str() methods should be created before any backend is called, so they can all share the code, but for the moment... If you want it to work, you can create your own str() method, and V will use it.

JalonSolov avatar Nov 25 '25 01:11 JalonSolov