B4J Question Map cannot be cast to Map$MyMap

xulihang

Well-Known Member
Licensed User
Longtime User
Hi there,

I am encountering this error. What is MyMap and why it cannot be cast to Map?
 
It is strange that when I log the object, I get MyMap while using GetType returns only Map.
 
Upvote 0
My code is a bit complicated. I will try to provide a sample project.

Currently, I use a workaround by sending a JSON string.
 
Upvote 0
You need to post the relevant code.
Here is the sample code:

B4X: Copy
Sub Button1_Click
    Wait For (returnMapORString) Complete (result As Object)
    Log(result)
    Log(GetType(result))'got Map instead of Map$MyMap
    If result Is Map Then
        Dim m As Map = result
        Log(m.Get("test"))
    End If
End Sub

Sub returnMapORString As ResumableSub
    Return CreateMap("test":"test")
End Sub
 
Upvote 0
Code: Copy
Sub returnMapORString As ResumableSub
	Dim ob As Object
	ob = CreateMap("test":"test")
	Return ob
End Sub
 
Upvote 1
Solution
**SEE POST #10

You can also save some memory typing by 'casting' the result eliminating the need for the 'dimmed' variable.
Code: Copy
    If result Is Map Then
        Log(result.As(Map).Get("test"))
    End If
 
Last edited:
Upvote 0
I stand corrected after checking the generated java. code.
 
Upvote 0
It is more than that, the JVM is very efficient and even if it did create an extra wrapper object (which is basically an object with a single pointer) the performance overhead will be insignificant. Now if it is inside a loop that is called million times then it makes sense to think about such mini-optimizations, but in the general case, such optimizations don't make any difference.
 
Upvote 0
Back
Top