-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description ## Problem Part of #5668 ## Summary I originally wanted to just add `type_of`, but in order to test it in some way I thought of comparing two types and checking whether they are equal or not... which required implementing `Eq` for `Type`, which I guess is useful any way. ## Additional Context Another way could have been to be able to turn a `Type` into a string and check that, but there's no unbounded `String` type at comptime... Another way would be to `println` the type and check the program's output, but there's no quick way to do it right now... but I think asserting on equality is fine too, especially because the implementation of `type_of` is very simple. ## Documentation\* Check one: - [ ] No documentation needed. - [ ] Documentation included in this PR. - [x] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
- Loading branch information
Showing
5 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use crate::cmp::Eq; | ||
|
||
impl Eq for Type { | ||
fn eq(self, other: Self) -> bool { | ||
type_eq(self, other) | ||
} | ||
} | ||
|
||
#[builtin(type_eq)] | ||
fn type_eq(_first: Type, _second: Type) -> bool {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "comptime_type" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.31.0" | ||
|
||
[dependencies] |
16 changes: 16 additions & 0 deletions
16
test_programs/compile_success_empty/comptime_type/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use std::meta::type_of; | ||
|
||
fn main() { | ||
comptime | ||
{ | ||
// Check type_of works correctly (relies on Eq for Type) | ||
let a_field = 0; | ||
let another_field = 1; | ||
let an_i32: i32 = 0; | ||
let field_type_1 = type_of(a_field); | ||
let field_type_2 = type_of(another_field); | ||
let i32_type = type_of(an_i32); | ||
assert(field_type_1 == field_type_2); | ||
assert(field_type_1 != i32_type); | ||
} | ||
} |