Closed fredemmott closed 4 years ago
I think this is a very worthwhile change.
(1) Hack is a big language and removing this would make learning the language easier. The syntax here is really weird and not used elsewhere (e.g. %my-category, :my-class+).
(2) There's no type checker support today, and we don't have any plans to implement the full attribute/children patterns in the type checker.
(3) Since the checking of children happens in userland code at runtime anyway, it would be more consistent to use normal code to define the constraints.
(Since XHP is very old and reified generics are very new, I'm sure there will be a few bugs when we initially exercise it FWIW.)
Today's hack nightlies add disable_xhp_children_declarations
hhconfig option
This is mostly done - though xhp-lib should be modified to throw an exception if an old-style children declaration is present.
My perspective
It feels like it's likely to stay in limbo.
With the introduction of reified generics, I think it can actually be implemented better as a standard Hack trait, without any special syntax.
Why is it weird?
pcdata
,any
, andempty
typesProposal
Rough idea:
XHPChild::anyNumberOf(XHPChild::ofType<:bar>())
is a bit verbose; the problem withXHPChild::anyNumberOf<:bar>()
or similar is patterns likechildren ((:bar | :baz)*)
- with the verbose syntax, they're something likeXHPChild::anyNumberOf(XHPChild::ofUnionType(XHPChild::ofType<:bar>(), XHPChild::ofType<:baz>())
, but I don't see a clean way to fit them in to something likeXHPChild::anyNumberOf<T>()
Migrations
This can be done automatically:
ReflectionXHPChildrenDeclaration
Verifying correctness
__xhpChildrenDeclaration()
Why do reified generics help?
They allow
XHPChild::of<T>()
consistently, rather thanXHPChild::string()
,XHPChild::instanceOfClass(classname<T>)
etcAdditionally,
classname<T>
is getting harder to use, as the typechecker gets stricter about unsafe usage.Why is it better?
XHPElementWithExactlyOneChildOfType<T>()
andXHPElementWithAnyNumberOfChildrenOfType<T>()
interfaces
much more practicalHow to start?