I'm trying to extract the data from a xml file produced by the Tiled2D map editor. I originally had an untyped function that loaded all of this data into a Hash, but I switched to a struct because I'm not going to be adding additional data to the object once it's loaded, and the Hash was causing me issues due to mixed types of the values (and the polymorphic issues). After some help in the Racket Discord from @samth (who along with others have been wonderfully helpful with all my TR questions so far) I thought I was starting to get it, but once I started testing the function out on a real file everything fell apart.
Once I load the data into the objs var, it becomes unusable with polymorphic functions because I'm dumb and can't understand how to use them even after reading through the TR guide. So I get this error below. I thought maybe if I annotated the objs var it would work, but it's challenging to get the typing right due to it being a nested list of lists of lists. I should probably back pedal to trying to use polymorphic functions in a simpler situation, but I don't even know where to begin. Anyways I've listed the error, function, and xml below. If someone could give me some pointers that would be helpful. Even if it's just some more basic examples of using TR with polymorphism that would be great.
; c:\home\racket\tmx\tmx.rkt:37:18: Type Checker: Polymorphic function `map' could not be applied to arguments:
; Domains: (-> a b ... b c) (Listof a) (Listof b) ... b
; (-> a c) (Pairof a (Listof a))
; Arguments: (All (a b c) (case-> (-> (List* a b c) b) (-> (Pairof a (Listof b)) b) (-> (Listof a) a))) (Listof (Listof Any))
;
; in: (map cadr objs)
; Context (plain; to see better errortrace context, re-run with C-u prefix):
; c:\Program Files\Racket\share\pkgs\typed-racket-lib\typed-racket\typecheck\tc-toplevel.rkt:414:0 type-check
; c:\Program Files\Racket\share\pkgs\typed-racket-lib\typed-racket\typecheck\tc-toplevel.rkt:653:0 tc-module
; c:\Program Files\Racket\share\pkgs\typed-racket-lib\typed-racket\tc-setup.rkt:101:12
; c:\Program Files\Racket\share\pkgs\typed-racket-lib\typed-racket\typed-racket.rkt:22:4
; c:\home\.emacs.d\offline-packages\racket-mode-master\racket\syntax.rkt:66:0
>
typed-xml.rkt is just a copy and paste of what's in the typed version of the xml lib and typed-xml-path.rkt is what is in the path file since they didn't make it into 8.4
#lang typed/racket/base
(require "typed-xml.rkt"
"typed-xml-path.rkt")
(: load-tmx (-> Path-String Tiled))
(define (load-tmx pth)
;; extract all the useful variables out of the tiled xml file and return an struct
;; Tiled xml contents
(define content (xml->xexpr (document-element
(read-xml (open-input-file pth)))))
;; (: objs (List (Listof (List Symbol (Listof (List Symbol String))))))
;; list of collision squares from the objectgroup section of the xml
(define objs (filter list? (se-path*/list '(objectgroup) content)))
(define col-lst (map cadr objs))
(define map-data (Tiled (assert (string->number (cast (se-path* '(map #:width) content) String)) exact-integer?)
(assert (string->number (cast (se-path* '(map #:height) content) String)) exact-integer?)
(assert (string->number (cast (se-path* '(map #:tilewidth) content) String)) exact-integer?)
(assert (string->number (cast (se-path* '(map #:tileheight) content) String)) exact-integer?)
(assert (string->number (cast (se-path* '(tileset #:firstgid) content) String)) exact-integer?)
(assert (cast (se-path* '(tileset #:source) content) String) string?)
(assert (cast (se-path* '(data #:encoding) content) String) string?)))
map-data)
xml file
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.8" tiledversion="1.8.1" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="24">
<tileset firstgid="1" source="Overworld.tsx"/>
<layer id="1" name="background" width="10" height="10" locked="1">
<data encoding="csv">
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1
</data>
</layer>
<layer id="2" name="midground" width="10" height="10" locked="1">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,45,46,0,0,0,
0,0,0,0,0,85,86,0,0,0,
0,0,0,0,0,0,0,891,0,0,
0,0,204,205,206,0,0,931,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,236,237,0,
0,0,0,0,0,0,0,276,277,0,
0,0,0,0,204,205,206,0,0,0,
0,0,0,0,0,0,0,0,0,0
</data>
</layer>
<objectgroup id="3" name="Collisions">
<object id="6" x="32.1424" y="64.1233" width="47.1638" height="15.5059"/>
<object id="8" x="63.4773" y="128.408" width="48.779" height="15.6674"/>
<object id="15" x="112.095" y="55.7243" width="15.5059" height="24.0664"/>
<object id="22" x="85.5879" y="33.2204" width="22.4979" height="12.3499"/>
<object id="23" x="86.641" y="26.5189" width="15.8922" height="6.1271"/>
</objectgroup>
</map>