Solution (source code)

= Solution

The nearest-point assertion requires \b[$F\ne\varnothing$]. If $F=\varnothing$, there is no possible point $q$, so the literal assertion for every closed subset has that exception.

For a nonempty closed subset $F$ of a <compact metric space>, $F$ is itself compact. The function $q\mapsto d(p,q)$ is continuous, since $|d(p,q)-d(p,q')|\leq d(q,q')$ by the <triangle inequality>. The <extreme value theorem> therefore gives a minimizer $q\in F$, with
$$
\boxed{d(p,q)=\inf_{q'\in F}d(p,q').}
$$

Now assume the midpoint property and suppose $X$ were disconnected. There would be nonempty disjoint relatively open sets $A,B$ with $X=A\cup B$. Each is also closed, hence compact. The continuous distance function on the compact product $A\times B$ attains its minimum $\delta=d(a,b)$. It is positive, since a zero value would imply $a=b$.

Choose a midpoint $m$ of this minimizing pair. It lies in either $A$ or $B$. In the first case $d(m,b)=\delta/2<\delta$ contradicts minimality; in the second, $d(a,m)=\delta/2<\delta$ does. Thus \b[the midpoint property forces $X$ to be connected]. The empty space is connected by the usual no-separation definition, so it also causes no exception here.