I'm with you that it must have been retractable, especially seeing someone kind of shoving it into the hat. I imagine the hat itself isn't as floppy as it appears, and is snugly holding the sword compressed.
It's crazy the kind of stuff that they chose to do practical in these movies, considering Voldermort's face is being CGIed every time it's on screen. It wouldn't be hard to fudge pulling a sword out of a hat. Kudos to that effects team!