1

Having a class Config with nullable boolean sAllowed and a interface function returns a nullable Config

interface IConfig {
    fun getConfig() : Config?
}

class Config (var sAllowed: Boolean?=null)

and when want to use this boolean:

var sAllowed = false
// some loop ...
    val config = iConfig.getConfig()
    if (config != null) {
        sAllowed = sAllowed || config.sAllowed == true
    }

but the config.sAllowed == true translates to:

Intrinsics.areEqual(config.getsAllowed(), true);

and the Intrinsics.areEqual(config.getsAllowed(), true); is:

public static boolean areEqual(Object first, Object second) {
    return first == null ? second == null : first.equals(second);
}

the first.equals(second); is :

public boolean equals(Object obj) {
    return (this == obj);
}

the doc for equals(Object obj) is Indicates whether some other object is "equal to" this one, not the value equality.

Is it sounds like the config.sAllowed == true is checking object equality not value, or what am I missing here?

     * The {@code equals} method for class {@code Object} implements
     * the most discriminating possible equivalence relation on objects;
     * that is, for any non-null reference values {@code x} and
     * {@code y}, this method returns {@code true} if and only
     * if {@code x} and {@code y} refer to the same object
     * ({@code x == y} has the value {@code true}).
     * <p>```
Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
lannyf
  • 9,865
  • 12
  • 70
  • 152

1 Answers1

2

It is checking Object equality. However look at the equals() of Boolean.java

public boolean equals(Object obj) {
        if (obj instanceof Boolean) {
            return value == ((Boolean)obj).booleanValue();
        }
        return false;
    }

This does consider the value wrapped by the Wrapper class.

first.equals(second);

Will invoke equals on Boolean and not Object since first is a Boolean.

Arka Prava Basu
  • 2,366
  • 3
  • 18
  • 34