What's the difference between PhantomData and the empty array?

  • Technical aspect:
    [T; 0] currently requires T : Sized, and [T; 0] has the alignment of T (whereas PhantomData always has an alignment of 1).

    • PhantomData<T> also unconditionally is Copy + Debug + Ord + Hash

    A tiny bonus for zero-long arrays is that they require no import or long name, and that instantiating one is very easy: [].

  • Documentation aspect:
    PhantomData may be a bit better at conveying that the stuff in question is "phantom", whereas an empty array, historically, is more often used for field/struct alignement shenanigans.

  • My own rule of thumb:
    I personally reach for PhantomData when inside data types, and for [; 0] when using it for function arguments, especially user-provided closure arguments when tinkering with higher-order signatures or whatnot (since the non-1-alignment of a zero-sized parameter should play no role whatsoever in the machine code generated around function calls)

10 Likes