facebook / facebook/hhvm

[Hack][Feature Request] add `scalar` type

Open
#8,485 1 comment 0 reactions 0 assignees View on GitHub
feature request
Dominant language
C++
Stars
18.7k
Forks
3.1k
Avg merge
1h 47m
Merged PRs (30d)
2

Description

following #8461, i think `scalar` is another type that should be included in hack.

```hack
type scalar = string|bool|num;
```

### why is `scalar` needed
`scalar` would be typically used for data storage ( e.g : session, cache, .. etc )

> `mixed` can be used instead

that's right, and that is the case now in @nuxed framework, but data is always serialized in a way or another, using `serialize()` or JSON encoding before being stored in cache as we don't have a way of storing data as-is.

`mixed` is not guaranteed to be "serializable" ( example of "un-serializable" types : `resource` / `object`s that don't allow serialization )

---

### covariance/contravariance

```hack
interface IFoo {
public function bar(): scalar;
}

enum Baz: string {
BAR = 'BAR';
}
```
```hack
class Foo1 implements IFoo {
// permitted, `string` is scalar
public function bar(): string { return 'baz'; }
}
class Foo2 implements IFoo {
// permitted, `num`, `float`, and `int` are `scalar`
public function bar(): num { return 12; }
}
class Foo3 implements IFoo {
// permitted, `bool` is `scalar`
public function bar(): bool { return false; }
}
class Foo4 implements IFoo {
// not sure about whether to include `enum` values in scalar
// but it makes sense as enum values can only be strings or integers.
public function bar(): Baz { return Baz::BAR; }
}
```
```hack
class Foo5 implements IFoo {
// Not permitted, `vec<_>` is not scalar.
public function bar(): vec { return vec['a']; }
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.