You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes it is useful to express a combination of generic types as a struct. For example, I use a struct like this in a key-value database that supports arbitrary types.
I wrote some generic functions that accept a ColumnFamily as an input, and use its contained types as the types of the parameters and return of the function. A simplified example:
This works great in zig, but zls has trouble inferring the type of any variable whose type is specified via ColumnFamily.
What Does Work?
Note: I have measured "successful type inference by zls" by checking that VS code shows a type hint for the variable.
ZLS has no trouble inferring types through layers of indirection, as long as they are supported forms of indirection.
type variable
You can assign a concrete type to a variable T of type type, and zls will successfully infer the concrete type of any variables that have their type specified as T
test"indirect type inference" {
constT: type=u8;
conststart: T=123;
constitem=start; // zls infers item as u8trystd.testing.expect(u8==@TypeOf(item));
}
generic function with type parameter
ZLS can infer the type returned from a generic function when the type is passed as a parameter to the function.
fnidentity(comptimeT: type, item: T) T {
returnitem;
}
test"double indirect type inference via type param" {
constT: type=u8;
constitem=identity(T, 123); // zls infers item as u8trystd.testing.expect(u8==@TypeOf(item));
}
generic function with anytype
The same also works when the parameter's type is inferred rather than being specified with a type parameter.
fnanyIdentity(item: anytype) @TypeOf(item) {
returnitem;
}
test"indirect type inferece via anytype fn" {
conststart: u8=123;
constitem=anyIdentity(start); // zls infers item as u8trystd.testing.expect(u8==@TypeOf(item));
}
What Doesn't Work?
If all you do is wrap a type in a struct and directly specify it as the type of a variable (similar to the first example, but now with a wrapper struct), ZLS cannot infer the type:
constTypeSpec=struct { T: type };
testTypeSpec {
constspec=TypeSpec{ .T=u8 };
conststart: spec.T=123;
constitem=start; // zls is unable to infer item's type heretrystd.testing.expect(u8==@TypeOf(item));
}
Likewise, it does not work when passed through a function, either.
constTypeSpec=struct { T: type };
fnspecIdentity(comptimespec: TypeSpec, item: spec.T) spec.T {
returnitem;
}
testspecIdentity {
constspec=TypeSpec{ .T=u8 };
constitem=specIdentity(spec, 123); // zls is unable to infer item's type heretrystd.testing.expect(u8==@TypeOf(item));
}
The text was updated successfully, but these errors were encountered:
Sometimes it is useful to express a combination of generic types as a struct. For example, I use a struct like this in a key-value database that supports arbitrary types.
I wrote some generic functions that accept a ColumnFamily as an input, and use its contained types as the types of the parameters and return of the function. A simplified example:
This works great in zig, but zls has trouble inferring the type of any variable whose type is specified via ColumnFamily.
What Does Work?
ZLS has no trouble inferring types through layers of indirection, as long as they are supported forms of indirection.
type
variableYou can assign a concrete type to a variable
T
of typetype
, and zls will successfully infer the concrete type of any variables that have their type specified asT
generic function with type parameter
ZLS can infer the type returned from a generic function when the type is passed as a parameter to the function.
generic function with anytype
The same also works when the parameter's type is inferred rather than being specified with a type parameter.
What Doesn't Work?
If all you do is wrap a type in a struct and directly specify it as the type of a variable (similar to the first example, but now with a wrapper struct), ZLS cannot infer the type:
Likewise, it does not work when passed through a function, either.
The text was updated successfully, but these errors were encountered: