Skip to content
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

Add a Callable type hint #352

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ The Koto project adheres to
- `export` can be used with multi-assignment expressions.
- e.g. expressions like `export a, b, c = foo()` are now allowed.
- Maps now support `[]` indexing, returning the Nth entry as a tuple.
- Objects that implement `KotoObject::call` can now be used in operations that
expect functions.
- `KotoObject::is_callable` has been added to support this, and needs to be
implemented for the runtime to accept the object as a function.

#### Core Library

Expand Down
11 changes: 11 additions & 0 deletions crates/cli/docs/language_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,17 @@ print! let x: Any = 'hello'
check! hello
```

#### `Callable`

The `Callable` type hint will accept functions, or any value that can behave
like a function.

```koto
let say_hello: Callable = || 'hello'
print! say_hello()
check! hello
```

#### `Indexable`

The `Indexable` type hint will accept any value that supports `[]` indexing.
Expand Down
10 changes: 10 additions & 0 deletions crates/runtime/src/types/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,17 @@ pub trait KotoObject: KotoType + KotoCopy + KotoEntries + KotoSend + KotoSync +
None
}

/// Declares to the runtime whether or not the object is callable
///
/// The `Callable` type hint defers to the function, expecting `true` to be returned for objects
/// that implement [KotoObject::call].
fn is_callable(&self) -> bool {
false
}

/// Allows the object to behave as a function
///
/// Objects that implement `call` should return `true` from [KotoObject::call].
fn call(&mut self, _ctx: &mut CallContext) -> Result<KValue> {
unimplemented_error("@||", self.type_string())
}
Expand Down
1 change: 1 addition & 0 deletions crates/runtime/src/types/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl KValue {
CaptureFunction(f) if f.info.generator => false,
Function(_) | CaptureFunction(_) | NativeFunction(_) => true,
Map(m) => m.contains_meta_key(&MetaKey::Call),
Object(o) => o.try_borrow().map_or(false, |o| o.is_callable()),
_ => false,
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/runtime/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2834,6 +2834,7 @@ impl KotoVm {
let value = self.get_register(value_register);
match self.get_constant_str(type_index) {
"Any" => true,
"Callable" => value.is_callable(),
"Indexable" => value.is_indexable(),
"Iterable" => value.is_iterable(),
expected_type => {
Expand Down
13 changes: 13 additions & 0 deletions crates/runtime/tests/object_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ mod objects {
Some(self.x.unsigned_abs() as usize)
}

fn is_callable(&self) -> bool {
true
}

fn call(&mut self, _ctx: &mut CallContext) -> Result<KValue> {
Ok(self.x.into())
}
Expand Down Expand Up @@ -592,6 +596,15 @@ x.as_number()
test_object_script(script, 256);
}

#[test]
fn callable() {
let script = "
let x: Callable = make_object 256
x()
";
test_object_script(script, 256);
}

#[test]
fn iterable() {
let script = "
Expand Down
15 changes: 15 additions & 0 deletions crates/runtime/tests/runtime_failures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ let x: String, y: Bool = 'abc', 123
);
}

#[test]
fn expected_callable() {
let script = "\
let foo: Callable = 99
# ^^^
";
check_script_fails_with_span(
script,
Span {
start: Position { line: 0, column: 4 },
end: Position { line: 0, column: 7 },
},
);
}

#[test]
fn expected_indexable() {
let script = "\
Expand Down
9 changes: 9 additions & 0 deletions crates/runtime/tests/vm_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,15 @@ true
check_script_output(script, true);
}

#[test]
fn callable_matches_functions() {
let script = "
let x: Callable = || true
x()
";
check_script_output(script, true);
}

#[test]
fn indexable_matches_indexable_values() {
let script = "
Expand Down
Loading