pedropark99 / zig-book

An open and introductory book for the Zig programming language (🚧 in construction 🚧)
https://pedropark99.github.io/zig-book/
Other
366 stars 10 forks source link

Paragraph about zig not being a strongly typed language due to type inference #45

Open rubin55 opened 3 hours ago

rubin55 commented 3 hours ago

Hi Pedro, I stumbled upon your book and started going through it. Great work, because I had a hard time getting started with Zig and this does seem to take me along for a ride so to speak, so cudos.

I wanted to give you my thoughts w/regards to this paragraph:

"Zig is not exactly a strongly-typed language. Because you can (if you want to) omit the type of an object in your code, if this type can be derived from the assigned value. But there are other situations where you do need to be explicit. For example, you do have to explicitly specify the type of each function argument, and also, the return type of every function you create in Zig. So, at least in function declarations, Zig is a strongly-typed language."

I think this is not worded exactly enough. Zig is a strongly typed language with type inference; type inference is what lets Zig infer types when omitted, but if it cannot for whatever reason, it will loudly complain. Anyways, I figured I'd share this point of view. Good luck with the book, I will continue working through it and if appreciated, share thoughts.

pedropark99 commented 2 hours ago

Hey @rubin55 ! Thank you for your comment! About this statement:

Zig is a strongly typed language with type inference; type inference is what lets Zig infer types when omitted, but if it cannot for whatever reason, it will loudly complain

You do have a fair point.

I'm a bit undecided as to what would be the most correct way to classify Zig in this sense. Because depending on the background (i.e. come from high-level languages, or from low-level languages) of the reader, he (or she) might see Zig in a different perspective. That is why I have used "is not exactly a strongly-typed language" in this matter.

However, I still think you are right when you say specifically "is not worded exactly enough". Because I say "you can (if you want to) omit the type of an object", and this is not exactly right. It would be more correct to say:

If the zig compiler can infer the type, you are allowed to omit it in the source code. Otherwise, you are forced to explicitly annotate the type.

Or something like that. Do you agree?

rubin55 commented 2 hours ago

Hi Pedro, that part of the paragraph I think is fine as is. I'm reading https://en.wikipedia.org/wiki/Strong_and_weak_typing and I see that it's not as clear-cut as I thought (i.e., there's some interpretation as to what constitutes strongly typed).

But as I've encountered it, for example in Java (since 10), C# (since 3.0), those are, just like Zig, considered strongly typed languages, which these days also support type inference. The fact that they have that does not make their type systems less strong.

You could also omit "strong" and use the more strict wording "static" (i.e., Zig is a statically typed programming language with type inference).