[Candidate] Contexts and CoEffects #8777
Replies: 3 comments 1 reply
-
There is now some early stage documentation at https://docs.hhvm.com/hack/contexts-and-capabilities/introduction - the feature has not been implemented yet, but this should be more approachable than the HIP - but with less precision and detail. |
Beta Was this translation helpful? Give feedback.
-
Thanks for working on this @DavidSnider. While I love this feature, I have 3 major concerns, and I hope my feedback doesn't feel too hursh syntaxThe syntax, is a bit weird, which might feel familiar to people writing C++, it doesn't for most people that would use hack ( people using higher-level programming languages: PHP, JavaScript .. etc ) But I don't really see an alternative syntax 😥 another issue is the class WithConstant {
const ctx C = [io];
public function has_io()[self::C]: void {
echo "I have IO!";
}
} instead of: class WithContextKeyword {
context C = [io];
public function has_io()[self::C]: void {
echo "I have IO!";
}
} why use
caller needs the same contextI'm working on a framework now, and from what I see, if I start adding context to all functions/methods, every application using this framework would need to list every single possible context on its entry point because the framework itself might call a function down the road that has that context, even if your application might now hit that code branch. This is in my opinion is weird, why do I need to define Pure functions, and immutable objects.Another issue I have with this feature is the way to declare "pure" functions and immutable objects. For example, I have the following immutable object: final class NamedAddress {
public function __construct(
private string $name,
private string $email,
) {}
public function withName(string $name): this {
$clone = clone $this;
$clone->name = $name;
return $clone;
}
public function withEmail(string $email): this {
$clone = clone $this;
$clone->email= $email;
return $clone;
}
public function getName(): string { return $this->name; }
public function getEmail(): string { return $this->email; }
} and the following pure function function address_to_string(NamedAddress $address): string {
return $address->getName().' <'.$address->getEmail().'>';
}
My question here, what context would I need to put to make hack understand that my object is immutable? I think declaring a pure context shouldn't be a thing, and immutability and purity should be a separate feature that probably e.g: final immutable class NamedAddress {
public function __construct(
public readonly string $name,
public readonly string $email,
) {}
public function withName(string $name): this {
$clone = clone $this; // creating a clone,
$clone->name = $name; // writing to readonly clone is okay, as long as the clone was created here.
return $clone; // returning a clone, means it properties are now locked back to readonly.
}
public function withEmail(string $email): this {
$clone = clone $this;
$clone->email= $email;
return $clone;
}
}
pure function address_to_string(NamedAddress $address): string {
return $address->getName().' <'.$address->getEmail().'>';
} Regards. |
Beta Was this translation helpful? Give feedback.
-
Hi, At present all available caps are within the default capability set. When we add a capability that does not fall within the set, we will make it clear how we intend to allow for entering a context with that capability. Most likely it will involve invoking a magic function that specifies you're entering a specific context. |
Beta Was this translation helpful? Give feedback.
-
HIP:
https://github.com/facebook/hhvm/blob/master/hphp/hack/doc/HIPs/contexts_and_coeffects.md
Related HIPs:
Beta Was this translation helpful? Give feedback.
All reactions