Skip to content

Instantly share code, notes, and snippets.

@radgeRayden
Last active December 11, 2019 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radgeRayden/32348343e8ed5d6acd578ce808845e90 to your computer and use it in GitHub Desktop.
Save radgeRayden/32348343e8ed5d6acd578ce808845e90 to your computer and use it in GitHub Desktop.
using import enum
@@ memo
inline member-typeof (T member)
let result =
static-if ((T < Struct) or (T < CStruct))
field-count := (countof T)
let result =
va-lfold none
inline (_ignore value computed-result)
let _key = (keyof value)
static-if (_key == member)
unqualified value
else
computed-result
elementsof T
elseif (T < Enum)
# TODO: this
# Unknown
else
# need branch to compile correctly; it doesn't matter that this compiles
# straight to an error because we want this inline to only have compile-time statements.
if true
hide-traceback;
error@ ('anchor `T) "while querying type information" "not an aggregate type."
# Unknown
static-if (none? result)
if true
hide-traceback;
error (.. "field " (tostring member) " not found in type " (tostring T) ".")
result
fn run-tests ()
using import struct
using import testing
struct A
A : i32
B : i32
C : u64
test (constant? (member-typeof A 'A))
test ((member-typeof A 'B) == i32)
test-error
member-typeof A 'doesnt-exist
enum B plain
A
B
C
test-error
member-typeof B 'A
static-if main-module?
run-tests;
locals;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment