V: Implicit declaration of function

Created on 25 Aug 2020  路  7Comments  路  Source: vlang/v

V version: V 0.1.29 995a5fe
OS: Windows 10

What did you do?

module main

import vweb
import json
import os

const (
    port = 8081
)

struct App {
pub mut:
    vweb vweb.Context
}

fn main() {
    mut spawns := []ItemSpawn{}
    load_item_spawns(mut spawns)

    vweb.run<App>(port)
}

fn load_item_spawns(mut spawns []ItemSpawn) {
    walk<ItemSpawn>("./data/items/spawns/", mut spawns)
}

fn parse_json<T>(file string, mut array []T)  {
    data := os.read_file(file) or {
        panic('error reading file $file')
        return
    }
    decoded_data := json.decode([]T, data) or {
        eprintln('Failed to parse item spawns')
        return
    }
    for d in decoded_data {
        array << d
    }
}

fn walk<T>(path string, mut array []T) {
    if !os.is_dir(path) {
        return
    }
    mut files := os.ls(path) or {
        return
    }
    for file in files {
        p := path + os.path_separator + file
        if os.is_dir(p) && !os.is_link(p) {
            walk(p, mut array)
        }
        else if os.exists(p) {
            parse_json(p, mut array)
        }
    }
}

pub fn (mut app App) init_once() {}

pub fn (mut app App) init() {}

pub fn (mut app App) index() {}

struct ItemSpawn {
    id          int
    amount           int = 1
    x int
    y int
}

What did you expect to see?
Compile.

What did you see instead?
Fails to compile:

PS E:\Sync\Insignia\game-api> v run gameapi.v
==================
  ^~~~~
C:\Users\Jonathan\AppData\Local\Temp\v\gameapi.tmp.c:14653:7: error: expected ';' before '_t478'
  array _t478 = files;
       ^~~~~~
       ;
C:\Users\Jonathan\AppData\Local\Temp\v\gameapi.tmp.c:14654:30: error: '_t478' undeclared (first use in this function); did you mean '_t479'?
  for (int _t479 = 0; _t479 < _t478.len; ++_t479) {
                              ^~~~~
                              _t479
C:\Users\Jonathan\AppData\Local\Temp\v\gameapi.tmp.c:14654:30: note: each undeclared identifier is reported only once for each function it appears in
C:\Users\Jonathan\AppData\Local\Temp\v\gameapi.tmp.c:14658:4: error: implicit declaration of function 'main__walk'; did you mean 'main__main'? [-Werror=implicit-function-declaration]
    main__walk(p, array);
...
==================
(Use `v -cg` to print the entire error message)

builder error:
==================
C error. This should never happen.

If you were not working with C interop, please raise an issue on GitHub:

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang
Bug

Most helpful comment

ATM it only infers T if that is the exact type for a function parameter. I think adding support for inferring T from []T should be quite simple.

All 7 comments

Error was misleading. Was missing <T> on generic func calls

C error always means a bug in the compiler. Also <> won't be needed in the calls.

Output from V has now changed to:

generic_6216.v:58:4: error: could not infer generic type `T` in call to `walk`
   56 |         p := path + os.path_separator + file
   57 |         if os.is_dir(p) && !os.is_link(p) {
   58 |             walk(p, mut array)
      |             ~~~~~~~~~~~~~~~~~~
   59 |         }
   60 |         else if os.exists(p) {
generic_6216.v:61:4: error: could not infer generic type `T` in call to `parse_json`
   59 |         }
   60 |         else if os.exists(p) {
   61 |             parse_json(p, mut array)
      |             ~~~~~~~~~~~~~~~~~~~~~~~~
   62 |         }
   63 |     }

@ntrel can you have a look please?

ATM it only infers T if that is the exact type for a function parameter. I think adding support for inferring T from []T should be quite simple.

Is this related to #6222 or is this a different problem?

I think it's a different problem, but haven't tested #6222 after the recent commits

Was this page helpful?
0 / 5 - 0 ratings