Skip to content

Instantly share code, notes, and snippets.

@fredemmott
Last active November 26, 2019 10:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredemmott/1e65842fadb9e66e2359 to your computer and use it in GitHub Desktop.
Save fredemmott/1e65842fadb9e66e2359 to your computer and use it in GitHub Desktop.
XHP namespace rules
<?hh
class :foo:bar {
// - xhp__foo_bar
// + foo\xhp__bar
}
<?hh
namespace foo;
class :bar {
// - \xhp__bar - this is a BC break
// + \foo\xhp__bar
//
// BC break unlikely to be a practical issue as `class :bar extends :x:element {}` doesn't work (references foo\xhp__x_element)
}
class :herp:derp {
// - \xhp__herp_derp - BC break
// + PARSE ERROR
// Arguments for making it an error instead of \foo\herp\xhp__derp:
// - allowing two namespace syntaxes is weird. We allow it in the global namespace for backwards compatibility
// - it looks like this creates a class in the 'foo' namespace. It would actually make a class in the \foo\herp namespace
// - it's currently pretty broken: you can define the class, but you can't reference (or extend) :x:element and friends
// Arguments for allowing it:
// - creating :foo and :foo:helper is currently common (however, :foo and :foo-helper is also perfectly valid)
// - it's a bit weird to allow it in the global namespace, but not others
// - possibly more appropriate as a lint error than a parser error
//
// We aren't entirely sure what the right approach is. We decided to make it an error as this lets us revisit it later;
// if we allowed it, we can't ban it in the future without breaking people's code.
}
<?hh
namespace foo;
$x = <x:frag />; // \foo\x\xhp__frag
$x = <:x:frag />; // \x\xhp__frag
use x as x;
$x = <x:frag />; // \x\xhp__frag
use hhvm\xhpbootstrap as bs;
$x = <bs:jumbotron />; // \hhvm\xhpbootstrap\xhp__jumbotron
$x = <:hhvm:xhpbootstrap:jumbotron />; // same
@fredemmott
Copy link
Author

Also need

<?hh
use \hhvm\xhpbootstrap\:jumbotron;

$x = <jumbotron />;

@azjezz
Copy link

azjezz commented Mar 7, 2019

Also

use foo\bar\{
  :herp, 
  :derp
};

$x = <herp><derp /></herp>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment