Sign in

    any

    Type-safe Any for WASM/JS/Native targets, without json/string serialization

    Any
    downcast
    Download zip
    Author
    Version
    0.2.1
    License
    Apache-2.0
    Last updated
    3 months ago
    Downloads
    569

    #Any

    no identity

    What's this? This module provides a type-safe Any type and a dyn_cast method, without JSON/string serialization.

    ///|
    test "Any and dyn_cast" {
    let a : @any.Any = Any(false)
    let b : @any.Any = Any([1, 2, 3])
    let c : Bool = a.dyn_cast()
    let d : Array[Int] = b.dyn_cast()
    let e : Result[Int, _] = try? b.dyn_cast()
    debug_inspect(c, content="false")
    debug_inspect(d, content="[1, 2, 3]")
    debug_inspect(
    e,
    content=(
    #|Err(Failure("failed to cast Array[Int] to Int"))
    ),
    )
    }

    #Advantages

    • True type safety
    • Supports all backends
    • No Json/String serialization or deserialization

    #Limitations

    • Requires manually implementing TypeInfo for custom types
    • Some boxing and conversion overhead for generic types

    #More examples

    • dyn_cast failed

      ///|
      test "dyn_cast failed" {
      let a = Any(false)
      debug_inspect(
      try? (a.dyn_cast() : Int),
      content=(
      #|Err(Failure("README.mbt.md:50:13-50:25@Yoorkin/any FAILED: failed to cast Bool to Int"))
      ),
      )
      }

    • implement TypeInfo for custom type

      ///|
      struct Pos {
      x : Int
      y : Int
      } derive(Debug)

      ///|
      extenum @any.Payload += {
      Pos(Pos)
      }

      ///|
      impl Anyable for Pos with fn iso() {
      {
      id: TypeId("my/pkg", "Pos", []),
      box: p => Pos(p),
      unbox: payload => {
      guard payload is Pos(p)
      p
      },
      }
      }

      ///|
      test {
      let a = Any({ x: 10, y: 20 })
      let b : Pos = a.dyn_cast()
      debug_inspect(b, content="{ x: 10, y: 20 }")
      }

    Anyable

    pub(open) trait Anyable :
    Debug
    {
    fn iso() -> Iso[Self]
    }

    impl Anyable for Bool
    impl Anyable for Int
    impl Anyable for Int16
    impl Anyable for Int64
    impl Anyable for UInt
    impl Anyable for UInt16
    impl Anyable for UInt64
    impl Anyable for Float
    impl Anyable for Double
    impl Anyable for String
    impl Anyable for Bytes
    impl Anyable for Json

    Any

    type Any

    impl Debug for Any

    Any::Any

    Any::dyn_cast

    #callsite(autofill(loc))
    fn[T : Anyable +
    Debug
    ] Any::dyn_cast(x : Any, loc~ : SourceLoc) -> T raise

    Any::id

    fn Any::id(self : Any) -> TypeId

    Any::unsafe_cast

    fn[T : Anyable +
    Debug
    ] Any::unsafe_cast(x : Any) -> T

    Iso

    pub(all) struct Iso[T] {
    id : TypeId
    box : (T) -> Payload
    unbox : (Payload) -> T
    }

    Payload

    pub extenum Payload {
    }

    TypeId

    pub struct TypeId {
    pkg : String
    ctor : String
    generics : Array[TypeId]
    } derive(Compare, Eq,
    Debug
    )

    impl Show for TypeId

    TypeId::TypeId

    fn TypeId::TypeId(pkg : String, ctor : String, generics : Array[TypeId]) -> TypeId

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io