Optionals for threads

This commit is contained in:
Bruce Hill 2024-09-11 12:56:16 -04:00
parent 908673c9d9
commit 02930b84f8
3 changed files with 25 additions and 1 deletions

View File

@ -3,6 +3,7 @@
#include "bool.h"
#include "datatypes.h"
#include "integers.h"
#include "thread.h"
#include "text.h"
#include "util.h"
@ -29,6 +30,8 @@ static inline bool is_null(const void *obj, const TypeInfo *non_optional_type)
return ((OptionalInt16_t*)obj)->is_null;
else if (non_optional_type == &Int8$info)
return ((OptionalInt8_t*)obj)->is_null;
else if (non_optional_type == &Thread)
return *(pthread_t**)obj == NULL;
switch (non_optional_type->tag) {
case ChannelInfo: return *(channel_t**)obj == NULL;

View File

@ -27,7 +27,9 @@ static CORD promote_to_optional(type_t *t, CORD code);
CORD promote_to_optional(type_t *t, CORD code)
{
if (t->tag == IntType) {
if (t == THREAD_TYPE) {
return code;
} else if (t->tag == IntType) {
switch (Match(t, IntType)->bits) {
case TYPE_IBITS8: return CORD_all("((OptionalInt8_t){.i=", code, "})");
case TYPE_IBITS16: return CORD_all("((OptionalInt16_t){.i=", code, "})");

View File

@ -61,6 +61,12 @@ func maybe_channel(should_i:Bool)->|Int|?:
else:
return !|Int|
func maybe_thread(should_i:Bool)->Thread?:
if should_i:
return Thread.new(func(): pass)
else:
return !Thread
func main():
>> 5?
= 5? : Int?
@ -200,6 +206,19 @@ func main():
fail("Truthy: $nope")
else: !! Falsey: $nope
do:
!! ...
!! Threads:
>> yep := maybe_thread(yes)
# No "=" test here because threads use addresses in the text version
>> nope := maybe_thread(no)
= !Thread
>> if yep: >> yep
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
if yep := maybe_int(yes):
>> yep
= 123 : Int