The `Any` type with dynamic typing and runtime type checks.
moon update
moon add tonyfettes/any// Store any value
let any = @any.of(42)
// Query type information
@json.inspect(any.type_name(), content="Int")
// Retrieve with type checking
let value : Int = any.to()
@json.inspect(value, content=42)
// Type mismatch raises TypeMismatch error
let value : Result[Bool, @any.TypeMismatch] = try! any.to()
@json.inspect(value, content={
"Err": { "TypeMismatch": { "expect": "Bool", "actual": "Int" } },
})let int_any = @any.of(42)
@json.inspect((int_any.to() : Int))
let str_any = @any.of("hello")
@json.inspect((str_any.to() : String))
let arr_any = @any.of([1, 2, 3])
@json.inspect((arr_any.to() : Array[Int]))let a0 = @any.of(42)
let a1 = @any.of(a0)
@json.inspect(physical_equal(a0, a1), content=true)let any = @any.of([1, 2, 3])
let info = any.type_info()
@json.inspect(
physical_equal(info.id(), @any.of([1]).type_info().id()),
content=true,
)
@json.inspect(info.name(), content="Array[Int]")let any = @any.of(42)
let id = any.type_id()
@json.inspect(physical_equal(id, @any.of(100).type_id()), content=true)let any = @any.of("hello")
@json.inspect(any.type_name())
let any = @any.of([1, 2, 3])
@json.inspect(any.type_name(), content="Array[Int]")
let any = @any.of(Some(42))
@json.inspect(any.type_name(), content="Int?")let any = @any.of(42)
let value : Int = any.to()
@json.inspect(value, content=42)
let wrong : Result[String, _] = try! any.to()
@json.inspect(wrong, content={
"Err": { "TypeMismatch": { "expect": "String", "actual": "Int" } },
})let any = @any.of([1, 2, 3])
let value : Array[Int]? = any.try_to()
@json.inspect(value is Some([1, 2, 3]), content=true)
let wrong : String? = any.try_to()
@json.inspect(wrong is None, content=true)// Only use when you're absolutely certain about the type
let any = @any.of(42)
let value : Int = any.unsafe_coerce()
@json.inspect(value, content=42)fn any_to_int(any : @any.Any) -> Int raise {
any
.map((int : Int) => int) // If Int, return as-is
.map((str : String) => @strconv.parse_int(str)) // If String, parse it
.map((double : Double) => double.to_int()) // If Double, convert it
.to() // Finally extract the Int
}
@json.inspect(any_to_int(@any.of(42)))
@json.inspect(any_to_int(@any.of("43")))
@json.inspect(any_to_int(@any.of(44.0)))let any = @any.of("hello")
any
.map((int : Int) => println("Int(\{int})"))
.map((str : String) => println("String(\{str})")) // This executes
.to() // Ensures all cases are handled///|
pub suberror TypeMismatch {
TypeMismatch(expect~ : TypeInfo, actual~ : TypeInfo)
}let any = @any.of([1, 2, 3])
let result : Result[Bool, @any.TypeMismatch] = try! any.to()
@json.inspect(result, content={
"Err": { "TypeMismatch": { "expect": "Bool", "actual": "Array[Int]" } },
})TypeMismatch: expected type 'Bool' but found type 'Array[Int]'{
"TypeMismatch": {
"expect": "Bool",
"actual": "Array[Int]"
}
}impl Show for TypeMismatchimpl ToJson for TypeMismatchtype Anytype TypeInfoThe `Any` type with dynamic typing and runtime type checks.