Ex02 - Generic return type

Moderatoren: pmueller, SE - Design and Construction

Azazel
Windoof-User
Windoof-User
Beiträge: 30
Registriert: 26. Apr 2017 19:10

Ex02 - Generic return type

Beitrag von Azazel »

Hi,

in the original Context-Class there is a HashMap<String, Object> making it possible to not only store values of class RefenceType but also for example simple Integer values. This is supported by a generic get-method adding new entries to the HashMap on demand.

However, I can not seem to get how write a generic get-method in the Scala implementation.
I am using a HashMap[String, Object] imported from java.util.HashMap. Just like in the Java implementation I have my get-method

Code: Alles auswählen

def get[T] (key: String, value: Supplier[T]): T = ...
but returning the computeIfAbsent-method in the body of the function results in an error message stating that "Expression of type Object doesn't conform to expected type T". Adding a cast to T before the statement does not solve the problem, since it only states "Cannot resolve symbol T".

What am I missing here?

eichberg
Moderator
Moderator
Beiträge: 448
Registriert: 25. Sep 2007 12:12
Kontaktdaten:

Re: Ex02 - Generic return type

Beitrag von eichberg »

In the original implementation you cannot really store primitive values in the map, they will always be wrapped in Integer objects.

In Scala, if you want to constrain the return type of a function to reference types only (see my other post in this forum for further information regarding Scala's type system), you have to write, e.g., def get[T <: AnyRef](...)

Azazel
Windoof-User
Windoof-User
Beiträge: 30
Registriert: 26. Apr 2017 19:10

Re: Ex02 - Generic return type

Beitrag von Azazel »

Thank you for your answer. However, I still have another question in this regard.

We were tasked to only test the functionality of the Context-Class, thus making it necessary to add some entries to the cache-HashMap. In the Java implementation, running this line of code

Code: Alles auswählen

int test = Context.get("test", () -> Context.getNextId());
is no problem, even though we are trying to add a Integer as a value to the HashMap<String, Object>. This makes it possible to write tests for the Context-Class without using the ReferenceType-Class. At which point does the compiler cast the Integer to Object?

Since it is not possible to run the above code in the Scala implementation (because an Integer does not extend AnyRef) how should we write the required tests without using any other classes? Or should we just add .asInstanceOf[AnyRef] to manually wrap the Integer with an Object?

eichberg
Moderator
Moderator
Beiträge: 448
Registriert: 25. Sep 2007 12:12
Kontaktdaten:

Re: Ex02 - Generic return type

Beitrag von eichberg »

The line:

Code: Alles auswählen

int test = Context.get("test", () -> Context.getNextId());
is the same (at runtime/in Bytecode) as the following:

Code: Alles auswählen

int test = Context.get("test", () -> Context.getNextId()).intValue();;
The intValue call is automatically added by the compiler

Azazel
Windoof-User
Windoof-User
Beiträge: 30
Registriert: 26. Apr 2017 19:10

Re: Ex02 - Generic return type

Beitrag von Azazel »

I am afraid I can not see how this information is supposed to help me understand why in Java the result of

Code: Alles auswählen

() -> getNextId()
it automatically boxed into an Integer-Object and therefore a valid value of the HashMap while in Scala it is not, thus the compiler is complaining about the result being an int and not an Object.

Antworten

Zurück zu „Software Engineering - Design and Construction“