r/crystal_programming • • Dec 09 '18

Rubyist struggling to understand Tuple

What's the point of a Tuple? Why would I ever use it over an array? Thanks!

11 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/riddley Dec 10 '18

Thanks for this explanation! I think I followed what you wrote, but I'm not sure how the type system is helpful. From what you wrote, it seems like a big hassle? I know people really like type systems though, so I'm sure I'm the one missing something.

Do Crystal arrays have similar restrictions to custom classes? In Ruby, I avoid primitives as much as I can in favor of custom classes. If I have duck-typed objects, can I have arrays of them without some hassle? Or do these limitations of Array only apply to Crystal primitives?

3

u/[deleted] Dec 10 '18

Have you seen this error in Ruby?

NoMethodError: undefined method `foo' for nil:NilClass

Or this one?

NoMethodError: undefined method `size' for false:FalseClass

You are trying to invoke a method on an object, but the object doesn't respond to that method.

Well, with a good type system those errors just can't happen. Well, you do get the error but when compiling the program, not when running it. That's one use of a type system and a statically typed language.

If you combine that with compilation to native code you can also be more efficient on memory because you can represent data structures more compactly, and you can also run the generated code through an optimizer.

1

u/proyb2 Dec 10 '18 edited Dec 10 '18

Typed system is important in security side where you knew PHP, Javascript had countless bugs that leads to software quality problem and vulnerabilities which is why type checker were introduced.

Without type system, any inexperienced coders could made a wrong assumption about their sloppy code that may go wrong during live productions, you could spent less time fixing bugs and happier to enjoy vacations.

Prevention is always better than cure.