8000 feat(`@bool`): add `Bool::to_int16` and `Bool::to_uint16` by illusory0x0 · Pull Request #2386 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(@bool): add Bool::to_int16 and Bool::to_uint16 #2386

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

Merged
merged 1 commit into from
Jul 2, 2025
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
50 changes: 50 additions & 0 deletions bool/bool.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,53 @@ pub impl Hash for Bool with hash(self) {
pub impl Hash for Bool with hash_combine(self, hasher) {
hasher.combine_bool(self)
}

///|
/// Converts a boolean value to an unsigned 16-bit integer.
///
/// Parameters:
///
/// * `self` : The boolean value to be converted.
///
/// Returns an unsigned 16-bit integer, where `true` is converted to 1 and
/// `false` is converted to 0.
///
/// Example:
///
/// ```moonbit
/// inspect(true.to_uint16(), content="1")
/// inspect(false.to_uint16(), content="0")
/// ```
///
pub fn to_uint16(self : Bool) -> UInt16 {
if self {
1
} else {
0
}
}

///|
/// Converts a boolean value to a 16-bit integer representation.
///
/// Parameters:
///
/// * `self` : The boolean value to be converted.
///
/// Returns a 16-bit integer, where `true` is converted to 1 and `false` is
/// converted to 0.
///
/// Example:
///
/// ```moonbit
/// inspect(true.to_int16(), content="1")
/// inspect(false.to_int16(), content="0")
/// ```
///
pub fn to_int16(self : Bool) -> Int16 {
if self {
1
} else {
0
}
}
2 changes: 2 additions & 0 deletions bool/bool.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ package "moonbitlang/core/bool"

// Types and methods
fn Bool::to_int(Bool) -> Int
fn Bool::to_int16(Bool) -> Int16
fn Bool::to_int64(Bool) -> Int64
fn Bool::to_uint(Bool) -> UInt
fn Bool::to_uint16(Bool) -> UInt16
fn Bool::to_uint64(Bool) -> UInt64
impl Hash for Bool

Expand Down
4 changes: 4 additions & 0 deletions bool/bool_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ test "bool convert" {
inspect(false.to_uint(), content="0")
inspect(true.to_uint64(), content="1")
inspect(false.to_uint64(), content="0")
inspect(true.to_uint16(), content="1")
inspect(false.to_uint16(), content="0")
inspect(true.to_int16(), content="1")
inspect(false.to_int16(), content="0")
}
0