B4J Question Map cannot be cast to Map$MyMap

xulihang

Active Member
Licensed User
Longtime User
Hi there,

I am encountering this error. What is MyMap and why it cannot be cast to Map?
 

xulihang

Active Member
Licensed User
Longtime User
I am using ABPlugin along with resumable sub to return a map.
 
Last edited:
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
It is strange that when I log the object, I get MyMap while using GetType returns only Map.
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
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

xulihang

Active Member
Licensed User
Longtime User
You need to post the relevant code.
Here is the sample code:

B4X:
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

Daestrum

Expert
Licensed User
Longtime User
B4X:
Sub returnMapORString As ResumableSub
	Dim ob As Object
	ob = CreateMap("test":"test")
	Return ob
End Sub
 
Upvote 1
Solution

Daestrum

Expert
Licensed User
Longtime User
**SEE POST #10

You can also save some memory typing by 'casting' the result eliminating the need for the 'dimmed' variable.
B4X:
    If result Is Map Then
        Log(result.As(Map).Get("test"))
    End If
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I stand corrected after checking the generated java. code.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
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
Top