Implementing a generic interface using a concrete type does not compile
Describe the bug
Using the implements keyword to explicitly signal that a struct implements a generic interface fails. The compiler tries to infer the interface type parameter, although the implementing struct passes a concrete type for the type parameter.
Reproduction Steps
Code: https://play.vlang.io/p/78dc5f1641
module main
struct System[T] {
data T
}
interface Transaction[T] {
mut:
run(mut system System[T])
}
// - - - - - - - - - -
struct Data {
dummy string
}
struct CreateDocumentTransaction implements Transaction[Data] { }
fn (mut txn CreateDocumentTransaction) run(mut system System[Data]) {
}
fn main() {
}
Expected Behavior
The code should compile, and the CreateDocumentTransaction struct which implements Transaction[Data] should be able to work with (have its run method invoked) System[Data].
Current Behavior
Output:
code.v:18:1: error: could not infer generic type `T` in interface `Transaction`
16 | }
17 |
18 | struct CreateDocumentTransaction implements Transaction[Data] { }
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 |
20 | fn (mut txn CreateDocumentTransaction) run(mut system System[Data]) {
Exited with error status 1
Possible Solution
No response
Additional Information/Context
No response
V version
V 0.4.10 19a666886454549f5931523839a93fde3e37f855.19a6668
Environment details (OS name and version, etc.)
|V full version |V 0.4.10 19a666886454549f5931523839a93fde3e37f855.19a6668
|:-------------------|:-------------------
|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.54GB/2.02GB
| |
|V executable |/home/admin/v/v
|V last modified time|2025-03-27 19:25:34
| |
|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 |Error: fatal: detected dubious ownership in repository at '/home/admin/v'
To add an exception for this directory, call:
git config --global --add safe.directory /home/admin/v
|.git/config present |true
| |
|cc version |cc (Debian 12.2.0-14) 12.2.0
|gcc version |gcc (Debian 12.2.0-14) 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+deb12u8) 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.
Connected to Huly®: V_0.6-22451
Some additional information; the code below works:
module main
struct System[T] {
data T
}
interface Transaction[T] {
mut:
run(mut system System[T]) System[T]
}
// - - - - - - - - - -
struct Data {
dummy string
}
struct CreateDocumentTransaction implements Transaction[Data] { }
fn (mut txn CreateDocumentTransaction) run(mut system System[Data]) System[Data] {
return system
}
fn main() {
}
By amending the Transation[T] interface to have the run signature return a System[T] rather than void, compilation succeeds. Briefly looking at https://github.com/vlang/v/blob/81b238e6f743b272c1703c3a2fe618ee4f2f3b4c/vlib/v/checker/interface.v it looks like it considers [T] being used in this scenarion, where T is used in a return value. But if only used by a "method" parameter of run, that is not enough.