this post was submitted on 20 Dec 2024
974 points (96.9% liked)

Programmer Humor

32751 readers
75 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] thedeadwalking4242@lemmy.world 6 points 1 week ago (1 children)

Recently I encountered an issue with “casting”. I had a class “foo” and a class “bar” that extended class foo. I made a list of class “foo” and added “bar” objects to the list. But when I tried use objects from “foo” list and cast them to bar and attempted to use a “bar” member function I got a runtime error saying it didn’t exists maybe this was user error but it doesn’t align with what I come to expect from languages.

I just feel like instead of slapping some silly abstraction on a language we should actually work on integrating a proper type safe language in its stead.

[–] masterspace@lemmy.ca 2 points 1 week ago* (last edited 1 week ago) (1 children)

I think that might be user error as I can't recreate that:

[–] pooberbee@lemmy.ml 1 points 1 week ago (1 children)

Yeah, you would get a runtime error calling that member without checking that it exists.

[–] masterspace@lemmy.ca 1 points 6 days ago* (last edited 3 days ago)

Because that object is of a type where that member may or may not exist. That is literally the exact same behaviour as Java or C#.

If I cast or type check it to make sure it's of type Bar rather than checking for the member explicitly it still works:

And when I cast it to Foo it throws a compile time error, not a runtime error:

I think your issues may just like in the semantics of how Type checking works in JavaScript / Typescript.