r/programming • • May 21 '20

Java programming language celebrates 25 years

https://www.infoworld.com/article/3544229/java-programming-language-celebrates-25-years.html
95 Upvotes

73 comments sorted by

View all comments

Show parent comments

1

u/DetriusXii May 22 '20

If Zig allows that, then you have higher kinded types. I'm not fully sure that zig's types are reified from the examples provided. Are you able to perform a cast of a function parameter into a generic type? C# allows that. Java does not. C# allows bool compare<Q>(object o) { return o instanceof Q; }

1

u/kprotty May 22 '20

Zig supports reified types at compile time, but not at runtime: https://ziglang.org/documentation/master/#typeInfo.

This would be the zig equivalent. The result would be known at compile time instead of runtime. object: var is essentially a generic for "any" type. Since Zig is a natively compiled language like C and doesn't support inheritance, this means that nothing is auto-boxed into objects and instanceof doesn't exist by default. Both of them could be implemented manually if the programmer wanted to though:

fn compare(comptime Q: type, object: var) bool {
  return @TypeOf(object) == Q;
}