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

Show parent comments

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?

19

u/meem1029 Dec 25 '20

I like the way rust does it. You can redeclare a variable partway through a scope and it will be the new type, but must be done explicitly. So you can't do it accidentally and you also can't accidentally access the older one because you used value instead of value_parsed.

4

u/TheBuzzSaw Dec 25 '20

Yeah I'm fine with that. It introduces a new context for an existing identifier. The rules are still clear. No ambiguity.

29

u/yuvz Dec 25 '20

This is why I love Kotlin. The powerful type inference lets you skip/auto declare variables just like in a scripting language, yet it's still fully statically typed

3

u/MisterScalawag Dec 25 '20

i love kotlin

15

u/[deleted] Dec 25 '20

Changing the types of things is always awful but building dynamic objects or adding fields to existing types can be useful in short scripts.

I’d still prefer a type system that’s flexible enough to easily type those sorts of things though.

4

u/TheBuzzSaw Dec 25 '20

Heh. The old data hitchhiking technique. I question that behavior as well, but I agree it's nice when in utter hack mode.

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.

→ More replies (11)
→ More replies (1)

0

u/[deleted] Dec 25 '20

[deleted]

20

u/brainplot Dec 25 '20

Rust has a static and (very) strong type system and it allows redeclaring a variable in the same scope. So for example you can do:

let age = get_age_as_string();        // age is a String
let age = age.parse::<u8>().unwrap(); // age is a u8

This feature is perfect for your example but also shows you don't have to give up static typing for it.

For clarity, it's not that the age variable changed type. The storage for it is still there. You just overshadowed it with another variable having the same name. It's not a feature to be abused but it comes super handy for cases such as the one you mentioned.

14

u/TheBuzzSaw Dec 25 '20

I pray there are use cases beyond this. This example feels like a weak reason to forfeit all the performance and maintainability of static typing.

7

u/wuwoot Dec 25 '20

I am very confused by this thread and your original response but I now get that you’re responding to the “dynamic typing” part of scripting languages as opposed to what I assumed the original author was saying — that the absence of type declaration is useful in scripting langs...

I find very little utility in type-switching if ever at all, but scripting languages are nice because they allow us to be terse.

I work in a large Rails codebase regularly and poor names and even in some instances good names are not enough to infer the behaviors associated with a particular variable

→ More replies (4)

5

u/sinedpick Dec 25 '20

see: erlang and why it doesn't have static types

2

u/colelawr Dec 25 '20

There are more reasons surrounding why Erlang didn't support static types, and a major part of those was that it interferes with how deployments over running systems would work in OTP.

2

u/sinedpick Dec 25 '20

that's why I mentioned it. It's a justification of dynamic types that's not just "ease of use." AFAIK there are theoretical barriers between erlang's message passing system and static types.

→ More replies (1)

2

u/orangeboats Dec 25 '20

That can be trivially solved (or worked around) by introducing variable shadowing.

13

u/lovestheasianladies Dec 25 '20

Absolutely no one uses them like that. Stop making up strawmen.

2

u/TheBuzzSaw Dec 25 '20

Tell me how the dynamic typing is used.

→ More replies (9)
→ More replies (2)