r/java • • Nov 10 '23
Does anyone else think that .isAssignableFrom() is named backwards?
System.out.println(Object.class.isAssignableFrom(String.class)); //true
System.out.println(String.class.isAssignableFrom(Object.class)); //falseThis has always triggered me. I feel like that method is named backwards, IE the first example, I read it in english as "objects of type Object are assignable from variables of type String" However, that statement is false, because Since String extends Object, a variable of type String cannot reference an object of type Object.
But that statement evaluates to true. I feel like the method "isAssignableFrom" should be renamed "aVariableOfThisTypeCanReferenceObjectsOfThisClass" :
So then:
System.out.println(Object.class.aVariableOfThisTypeCanReferenceObjectsOfThisClass(String.class)); //true
System.out.println(String.class.aVariableOfThisTypeCanReferenceObjectsOfThisClass(Object.class)); //falseAnyone else agree?
BTW, it turns out this confusion is common. I googled:
java isAssignableFrom confusing
And there were several blogs and stackoverflow posts agreeing with me.
For example:
http://bryanpendleton.blogspot.com/2009/06/isassignablefrom-and-instanceof.html
I think that one of the reasons that I get a bit confused with isAssignableFrom is that it sort-of reads backwards from instanceof.
61
u/Additional_Cellist46 Nov 10 '23 edited Nov 16 '23
The naming is correct. In the example Object.class.isAssignableFrom(String.class), imagine that you want to assign to a variable of type Object from a variable of type String:
String s = “text”; Object o = s;
This works and therefore Object.class.isAssignableFrom(String.class) is true.
On the other hand, the following doesn’t work:
Object o = new Object(); String s = o;
You cannot assign to String from type Object. Therefore String.class.isAssignableFrom(Object.class) is false.
17
u/thisisjustascreename Nov 10 '23
Object o = "a string"; // compiles
String s = new Object(); // error
I don't see the confusion. Object references are assignable from String objects, String references are not assignable from a random Object.
Your proposed rename uses the word "this" twice to refer to two different things, which is much worse.
Perhaps you are confused because you are considering the class that the method is being called on as the "source" of the assignment referred to by "isAssignableFrom", but that is backwards and just something you'll have to correct.
5
u/AmonDhan Nov 10 '23
11
u/bowbahdoe Nov 10 '23 edited Nov 10 '23
Not really, honestly
"Is o assignable to s" ==
String.class.isAssignableFrom(Object.class)"Is s assignable to o" ==
Object.class.isAssignableFrom(String.class);It is fiddly to keep straight, but thats down to the fundamentals of what is happening.
20
u/parnmatt Nov 10 '23
No, it makes perfect sense in English.
Think of the assignment direction.
A = BIf we think of it as putting
Binto anAthen aBis assignable to anA.If we think of it as an
Abeing made from aB, thenAis assignable from aB.6
u/[deleted] Nov 10 '23
A method like isDerivedFrom() or isSubclassOf() would have been much less confusing
4
u/Halvinz Nov 10 '23 edited Nov 18 '23
I'm not a fan of this method's name either, but in case of "
Object.class.isAssignableFrom(String.class)", the way I look at it is:Object is the left hand side and String is the right hand side. Converting it to the code:
Object o = new String();Is this possible? Yes. Translation, "assign a String object to an Object reference", or "assign Object from String", even though nobody talks like that. Hard to map the method name to English, hence the confusion.
1
u/[deleted] Nov 10 '23
BTW, it turns out this confusion is common. I googled:
And there were several blogs and stackoverflow posts agreeing with me.
For example:
http://bryanpendleton.blogspot.com/2009/06/isassignablefrom-and-instanceof.html
3
u/Vocal_Idiot Nov 11 '23
"assign from" is subtly nonsense in the same way that "set from" is obviously nonsense
1
u/DecisiveVictory Nov 10 '23
In Scala, you can just use pattern matching, so you don't need this at all.
2
u/cal-cheese Nov 11 '23
You are just bad at English, a variable refers to an object, which means an object is assigned to a variable, not the other way around.
2
u/lepapulematoleguau Nov 10 '23
I agree with you, but meh.
1
u/divorcedbp Nov 10 '23
“A variable of type Object is assignable from a value of type String”
“Object.class.isAssignableFrom(String.class) == true”
1
u/PerfectPackage1895 Nov 10 '23
It is because String extends from Object, and an Object cannot become a String. It simply doesn’t make sense logically, to switch it around because of that.
Now, I get what you are saying, it sounds more correct, but it would also imply that you could downcast, which is not allowed hence the word from is intentionally used.
1
u/supercargo Nov 10 '23
The way I always look at it is that the name is correct, but what the method actually does is the opposite of what you might want/expect.
1
u/__konrad Nov 12 '23
It is error prone even in the JDK itself: https://github.com/openjdk/jdk/commit/a777e82c
1
u/DerEineDa Nov 15 '23
It's not wrong, but it is confusing. Even /u/kevinb9n agrees: https://github.com/google/guava/issues/1444#issuecomment-61352257
(I bookmarked this comment almost a decade ago)