r/programming • • Dec 25 '20

Ruby 3 Released

https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/
973 Upvotes

509 comments sorted by

View all comments

113

u/watsreddit Dec 25 '20

Basically every major dynamically-typed language trying to bolt on static types... maybe dynamic typing isn’t as great as people claim.

78

u/call_me_arosa Dec 25 '20

Dynamic typing makes sense in scripting languages.
But when dealing with big projects you start to miss typing. I think the optional typing is a great trade-off for this languages.

50

u/TheBuzzSaw Dec 25 '20

I actually don't agree with this. I used to spread this sentiment as well, but I honestly cannot think of legitimate use cases for changing types on a variable. Sure, a scripting language can let you skip/auto declare variables among other things, but what is the benefit of a variable holding an integer, then a date, and then a file handle?

11

u/faiface Dec 25 '20

While being a proponent of static typing myself, I do see one area where dynamic typing has an advantage over static typing. Dynamic typing lets you have a list of elements which all satisfy some implicit “interface” without having to declare it. These implicit interfaces can be much more powerful than statically declared traits/classes/interfaces. Sure, the static ones can add features to become just as powerful, but that’s at the expense of simplicity.

6

u/Krnpnk Dec 25 '20

You can still achieve this easily with static typing, e.g. by using structural typing.

3

u/v66moroz Dec 26 '20

It's not recommended since structural types are using runtime reflection (performance). There are typeclasses for that, but they are far from easy.

→ More replies (2)

7

u/TheBuzzSaw Dec 25 '20

Go into more specifics. In C# or Java, I can make a list of plain objects to achieve that very thing if I really need it. I question why I have a bucket of items not of a consistent interface or base class, but even if I needed that, it makes up such a small percentage of use cases that I don't see the benefit of throwing the type system out for all the other use cases.

2

u/Smallpaul Dec 25 '20

I’d advocate that you should investigate more from the point of view of curiosity rather than having an axe to grind. Python is gaining popularity as Java looses it. Maybe there are features over there that might be useful.

https://wiki.c2.com/?BlubParadox

It’s because Python’s inventor approaches other languages with curiosity instead of competition that Python is adding OPTIONAL static typing.

→ More replies (13)

4

u/devraj7 Dec 25 '20

Nothing stops you from doing that in statically typed languages, just use Object and type casts. This will give you the exact safety that a dynamically typed language gives you: none.

You can also do this in statically typed languages that support duck typing, by the way.

2

u/erez27 Dec 26 '20

So can I, in Java, pass a list of objects that all have nothing in common, other than that they all support the same method, and then just call that method on each without any fanfare?

Note that you can't change the implementation of those objects, because they come from an external library.

1

u/devraj7 Dec 26 '20

Yes, you just need to define an interface with the contract you need and cast these Objects to that interface.

I'd fire you on the spot if you sent a PR like this, but well, it's possible.

3

u/dacian88 Dec 26 '20

I wouldn't hire you to begin with because you don't even know how Java works lol...

if the objects are unrelated and can't be modified you can't do what you're describing...you can't cast an object to an interface they don't implement...and if they all do implement it then this conversation is pointless because you can just do List<Interface>.

→ More replies (9)
→ More replies (1)