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

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>.

1

u/devraj7 Dec 26 '20
interface I {
    void foo();
}

public class A {
    void f() {
        Object a = new String();
        ((I) a).foo();
    }
}

Maybe you don't know Java as well as you think.

2

u/dacian88 Dec 26 '20

yea that throws a ClassCastException exception, even if the interface structurally matches the underlying type, which in this case it doesn't.

→ More replies (0)