doc.odin 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Utility procedures and types to perform runtime type introspection/reflection (`RTTI`).
  2. //
  3. // WARNING! THIS IS ADVANCED BEHAVIOUR FOR ODIN! THIS SHOULD NOT BE USED BY BEGINNERS TO ODIN!
  4. //
  5. // This package is only to be used by individuals who know exactly how the RTTI works as well as EXACTLY how `any` works.
  6. // Especially since `any` can be unintuitive in its use to many, it can be dangerous to use. It is highly recommend that you **do not**
  7. // use `any` unless you know exactly what you are doing.
  8. //
  9. // RTTI is an extremely powerful tool which should only be used when absolutely necessary (such runtime-type-safe formatted printing).
  10. //
  11. // ## The Type System of Odin
  12. //
  13. // It is important to understand how the type systems works in Odin before using any RTTI. A good example of this is Odin's `distinct` type system.
  14. // In Odin, `distinct` types are represented by `Type_Info_Named`. A named struct is a `Type_Info_Named` which then points to a `Type_Info_Struct`.
  15. // This means you must use something like `type_info_base` to restrict the `Type_Info_Named` aspect and get the base-type directly. Doing a type-assertion
  16. // on the variant will not work as (incorrectly) expected without doing this.
  17. //
  18. // ## Advanced Information of How `any` Works
  19. //
  20. // An overview of how `any` works:
  21. //
  22. // An `any` type can reference any data type. It is functionally equivalent to `struct {data: rawptr, id: typeid}` with extra semantics on
  23. // how assignment and type assertion works.
  24. //
  25. // This is commonly used to construct runtime-type-safe printing, such as in `core:fmt`.
  26. // The use of `any` outside of this is heavily discourage and should be only used by people who FULLY understand its semantics.
  27. //
  28. // The `any` value is only valid as long as the underlying data is still valid. Passing a literal to an `any` will allocate the literal in
  29. // the current stack frame.
  30. //
  31. // Example:
  32. // x: int = 123
  33. // a: any = x
  34. // // equivalent to
  35. // a: any
  36. // a.data = &x
  37. // a.id = typeid_of(type_of(x))
  38. // // With literals
  39. // v: any = 123
  40. // // equivalent to
  41. // v: any
  42. // _tmp: int = 123
  43. // v.data = &_tmp
  44. // v.id = typeid_of(type_of(_tmp))
  45. //
  46. //
  47. // `any` is a topologically-dual to a `union` in terms of its usage. Both support assignments of differing types
  48. // (`any` being open to any type, `union` being closed to a specific set of types), type assertions (`x.(T)`), and `switch in`.
  49. // The main internal difference is how the memory is stored. `any` being open is a pointer+typeid, a `union`
  50. // is a blob+tag. A `union` does not need to store a `typeid` because it is a closed ABI-consistent set of variant types.
  51. package reflect