-
Notifications
You must be signed in to change notification settings - Fork 7.9k
arraykey
type implementation
#4073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Not sure this is worth it, especially due to the BC break. Gets solved by union types: |
@Majkl578, from Hack lang docs :
see : https://docs.hhvm.com/hack/types/arraykey also : #1887 aside from that, i'll be trying to implement generics in the future ( not any time soon though ), specially on existing functions / interfaces. e.g : // `Tv` has no type constrain = `as mixed`
ArrayAccess<Tk as arraykey, Tv> {
abstract public offsetExists ( Tk $offset ) : bool
abstract public offsetGet ( Tk $offset ) : Tv
abstract public offsetSet ( ?Tk $offset , Tv $value ) : void
abstract public offsetUnset ( Tk $offset ) : void
} // same behavoir as now,
// same as implements ArrayAccess<arraykey, mixed>
class UntypedStorage implements ArrayAccess { /* */ }
// ok, `string` is `arraykey`
class StringFooStorage implements ArrayAccess<string, Foo> { /* */ }
$storage['foo'] = new Foo();
// ok, `int` is `arraykey`
class IntBarStorage implements ArrayAccess<int, Bar> { /* */ }
$storage[1] = new Bar();
// type error, `Baz` is not `arraykey`
// you can't do `$storage[new Baz] = new Qux` anyway ( Illegal offset )
// so we raise a type error at declaration.
class BazQuxStorage implements ArrayAccess<Baz, Qux> { /* */ } |
Even if we'd like to introduce As such, I don't think we can really introduce |
@azjezz as Nikic said, this might be difficult to get passed in an RFC. Before spending much time on drafting an RFC, gauging the reaction of people to the idea on the internals would probably be a good thing to do, though it would probably get the same sort of response. If nothing else, this would be a useful push to show how the union types https://wiki.php.net/rfc/union_types should probably be re-visited. |
@azjezz Are you planning on proposing an RFC for this? We have union types now, which seem to handle this case reasonably. |
@iluuu1994 no, union is good enough, and handles the int|string vs string|int problem better. |
TODO :
arraykey
type