I am trying to use AMS blackboard bold font together with TG Pagella Math in the way given in this answer. But I got the error
! Internal error: bad native font flag in `map_char_to_glyph'
Example (xelatex):
\documentclass{article}
\usepackage{mathtools}
\usepackage{unicode-math}
\setmathfont{TeX Gyre Pagella Math}
\DeclareSymbolFont{AMSb}{U}{msb}{m}{n}
\protected\def\mathbb#1{{\mathchar\numexpr256*\symAMSb+`#1\relax}}
\begin{document}
$\hat{\mathbb{Z}}$
\end{document}
It seems that there is a similar question here. I would like to know whether there are some better workaround to this problem.

$\mathbb{XYZ}$with and without your command. – MaxNoe Nov 29 '14 at 16:54\mathbb{XYZ}withpdflatex? The spacing is the same as you get with my code. The TeX Gyre math fonts add spacing around blackboard bold letters, AMS doesn't. – egreg Nov 29 '14 at 16:57