v
v copied to clipboard
Two functions returning a generic type, one casted and the other as generic, the specific type will overwrite the other
Describe the bug
Code: https://vosca.dev/p/c4dc2948aa
struct FieldError {
}
type Validator[T] = fn (data T) ?FieldError
fn get_max_validator[T](max_value T) Validator[T] {
return fn [T](data T) ?FieldError {
return none
}
}
fn get_max_length_validator(length int) Validator[string] {
return fn (data string) ?FieldError {
return FieldError{}
}
}
const validate_int = get_max_validator[int](10)
validate_int(12345)
Expected Behavior
It should compile, and detect correctly that validate_int is a Validator[int] not a Validator[string]
If you cast the validate_int as Validator[int] inside an unsafe block works fine (If I remember correctly)
Current Behavior
Output:
code.v:23:14: error: cannot use `int literal` as `string` in argument 1 to `validate_int`
21 | const validate_int = get_max_validator[int](10)
22 |
23 | validate_int(12345)
| ~~~~~
code.v:8:9: error: cannot use `fn (T) ?FieldError` as type `fn (string) ?FieldError` in return argument
6 |
7 | fn get_max_validator[T](max_value T) Validator[T] {
8 | return fn [T](data T) ?FieldError {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 | return none
10 | }
Exited with error status 1
Reproduction Steps
Simly try to build it
Possible Solution
If both factory functions, are generics everything works fine.
You can see my code as everything generic here: https://github.com/Dracks/vlang-validator-form/blob/main/attributes.v#L38
Additional Information/Context
No response
V version
V 0.3.3 fc4c431.bbfa25a
Environment details (OS name and version, etc.)
V full version: V 0.3.3 fc4c431.27e1c20
OS: linux, Ubuntu 22.04.2 LTS
Processor: 2 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz
getwd: /home/pmakhnev/playground
vexe: /home/pmakhnev/v/v
vexe mtime: 2023-04-28 00:00:10
vroot: OK, value: /home/pmakhnev/v
VMODULES: OK, value: /root/.vmodules
VTMP: OK, value: /tmp/v_0
Git version: git version 2.34.1
Git vroot status: weekly.2023.17-5-g27e1c20e (4 commit(s) behind V master)
.git/config present: true
CC version: cc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3