New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get type define location when reading code/at compile time/at runtime? #1202

Closed
bronze1man opened this Issue Jul 6, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@bronze1man

bronze1man commented Jul 6, 2018

I have following code (h.zig):

const std = @import("std");

pub fn main() !void {
    var stdout_file = try std.io.getStdOut();
    std.debug.warn("{}\n",@typeName(@typeOf(stdout_file)) );
    var file2 = File{};
    std.debug.warn("{}\n",@typeName(@typeOf(file2)));
}

const File = struct{
};

Then I got:

File
File

So How Do I know where the type of stdout_file is defined and where the type of file2 is defined since the both named File?
If I search const File = in a big code base. I may find 100+ of them, I will cost time to find the real defined location.
One thing I like golang is that you can use reflect or simple read the code of the "Import" part and search in directory to find the define of the type.

I think zig do not do this stuff good. You can use const to assign type mulit times. And this will make the reader crazy.

Here is the path to get the define location of the type of stdout_file:

  • find std.io.getStdOut() in h.zig
  • find std in h.zig get const std = @import("std");
  • find std/index.zig file to located @import("std")
  • find io in std/index.zig file get pub const io = @import("io.zig");
  • find std/io.zig file to located @import("io") in std/index.zig file
  • find getStdOut in std/io.zig file, get pub fn getStdErr() GetStdIoErrs!File {
  • find File in std/io.zig file get const File = std.os.File;
  • find os in std/index.zig file get pub const os = @import("os/index.zig");
  • find File in std/os/index.zig file get pub const File = @import("file.zig").File;
  • find File in std/os/file.zig file get pub const File = struct {

After those 10 steps I finally find the define of the type File.

Base on that the @typename can return same string to different types. I think I can not get type define location at compile time/at runtime to help me print a helpful debug information.

Of cause, One way to solve this problem is try to make the type name globally unique like zigFile or tpFile. And search the type name.
But the compiler do not check whether the type name globally unique is true or not. So I will fail into the search type name problem sometime even if i try to use globally unique name.

The zig std library is following a good rule about the type define file path and the code to include the type. But the careless/bad programmers always exist. If the compiler do not have some rules about the file path and the code to include them, the careless/bad programmers will make the project will difficult to read.

Since zig have a zen that Favor reading code over writing code.. I think easy to find type define location should be a huge help to reading code easy.

@bronze1man

This comment has been minimized.

bronze1man commented Jul 6, 2018

Search type define location when reading code will became easy once we have a zig IDE.
But it is still a big problem at compile time/at runtime even if we have a zig IDE.
And the code size of refactor tool or ide will be big base on all those complex stuff.

@andrewrk

This comment has been minimized.

Member

andrewrk commented Jul 6, 2018

After the above commit:

const a = @import("a.zig");
const b = @import("b.zig");

test "aoeou" {
    var a1: a.Foo = undefined;
    bar(&a1);
}

fn bar(x: *b.Foo) void {}
/home/andy/downloads/zig/build/test.zig:6:10: error: expected type '*Foo', found '*Foo'
    bar(&a1);
         ^
/home/andy/downloads/zig/build/test.zig:6:10: note: pointer type child 'Foo' cannot cast into pointer type child 'Foo'
    bar(&a1);
         ^
/home/andy/downloads/zig/build/b.zig:1:17: note: Foo declared here
pub const Foo = struct {
                ^
/home/andy/downloads/zig/build/a.zig:1:17: note: Foo declared here
pub const Foo = struct {
                ^

Now it tells you where structs are defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment