Add compile time ternary $val ??? <expr> : <expr>.

This commit is contained in:
Christoffer Lerno
2025-08-28 01:56:05 +02:00
parent 90d3f429aa
commit 47316dac59
15 changed files with 86 additions and 26 deletions

View File

@@ -546,17 +546,10 @@ macro bool @is_valid_list(#expr) @const
macro bool @is_valid_fill(left, right, fill_with)
{
if (@is_empty_macro_slot(fill_with)) return true;
usz left_len = @select($defined(left.len()), left.len(), left.len);
usz right_len = @select($defined(right.len()), right.len(), right.len);
usz left_len = $defined(left.len()) ??? left.len() : left.len;
usz right_len = $defined(right.len()) ??? right.len() : right.len;
if (left_len == right_len) return true;
return left_len > right_len ? $defined(($typeof(right[0]))fill_with) : $defined(($typeof(left[0]))fill_with);
}
macro usz find_len(list)
{
$if $defined(list.len()):
return list.len();
$else
return list.len;
$endif
}
macro usz find_len(list) => $defined(list.len()) ??? list.len() : list.len;

View File

@@ -204,10 +204,10 @@ fn void StderrLogger.log(&self, LogPriority priority, LogCategory category, LogT
}
alias LogFn = fn void(void*, LogPriority priority, LogCategory category, LogTag tag, String file, String function, int line, String fmt, any[] args);
LogFn current_logfn = @select(env::LIBC, (LogFn)&StderrLogger.log, (LogFn)&NullLogger.log);
LogFn current_logfn = env::LIBC ??? (LogFn)&StderrLogger.log : (LogFn)&NullLogger.log;
OnceFlag log_init;
Mutex logger_mutex;
Logger current_logger = @select(env::LIBC, &stderr_logger, &null_logger);
Logger current_logger = env::LIBC ??? &stderr_logger : &null_logger;
StderrLogger stderr_logger @if (env::LIBC);
NullLogger null_logger;
LogPriority[256] config_priorities = { [0..255] = ERROR, [CATEGORY_APPLICATION] = INFO, [CATEGORY_TEST] = VERBOSE, [CATEGORY_ASSERT] = WARN};

View File

@@ -43,7 +43,7 @@ macro promote_int(x)
@param #value_2
@returns `The selected value.`
*>
macro @select(bool $bool, #value_1, #value_2) @builtin
macro @select(bool $bool, #value_1, #value_2) @builtin @deprecated("Use '$bool ? #value_1 : #value_2' instead.")
{
$if $bool:
return #value_1;

View File

@@ -193,9 +193,9 @@ fn usz? Socket.peek(&self, char[] bytes) @dynamic
enum SocketShutdownHow : (CInt native_value)
{
RECEIVE = @select(env::WIN32, libc::SD_RECEIVE, libc::SHUT_RD),
SEND = @select(env::WIN32, libc::SD_SEND, libc::SHUT_WR),
BOTH = @select(env::WIN32, libc::SD_BOTH, libc::SHUT_RDWR),
RECEIVE = env::WIN32 ??? libc::SD_RECEIVE : libc::SHUT_RD,
SEND = env::WIN32 ??? libc::SD_SEND : libc::SHUT_WR,
BOTH = env::WIN32 ??? libc::SD_BOTH : libc::SHUT_RDWR,
}
fn void? Socket.shutdown(&self, SocketShutdownHow how)