Jump to: TypeClass Bool Integer Float Complex Imaginary Class Pointer Array Other Box unboxable type data toString opEquals opCmp toHash box boxArray boxArrayToArguments UnboxException object outputType unboxCastReal unboxCastInteger unboxCastComplex unboxCastImaginary unbox
This module is a set of types and functions for converting any object (value or heap) into a generic box type, allowing the user to pass that object around without knowing what's in the box, and then allowing him to recover the value afterwards.
WARNING:
This module is being phased out. You may want to use std.variant for new code.
Example:
// Convert the integer 45 into a box.
Box b = box(45);
// Recover the integer and cast it to real.
real r = unbox!(real)(b);
That is the basic interface and will usually be all that you need to understand. If it cannot unbox the object to the given type, it throws UnboxException. As demonstrated, it uses implicit casts to behave in the exact same way that static types behave. So for example, you can unbox from int to real, but you cannot unbox from real to int: that would require an explicit cast.
This therefore means that attempting to unbox an int as a string will throw an error instead of formatting it. In general, you can call the toString method on the box and receive a good result, depending upon whether std.string.format accepts it.
Boxes can be compared to one another and they can be used as keys for associative arrays.
There are also functions for converting to and from arrays of boxes.
Example:
// Convert arguments into an array of boxes.
Box[] a = boxArray(1, 45.4, "foobar");
// Convert an array of boxes back into arguments.
TypeInfo[] arg_types;
void* arg_data;
boxArrayToArguments(a, arg_types, arg_data);
// Convert the arguments back into boxes using a
// different form of the function.
a = boxArray(arg_types, arg_data);
One use of this is to support a variadic function more easily and robustly; simply call "boxArray(arguments, argptr)", then do whatever you need to do with the array.
Authors:
Burton Radons
License:
Public Domain
enum TypeClass;
The type class returned from Box.findTypeClass; the order of entries is important.
Bool
< bool
Integer
< byte, ubyte, short, ushort, int, uint, long, ulong
Float
< float, double, real
Complex
< cfloat, cdouble, creal
Imaginary
< ifloat, idouble, ireal
Class
< Inherits from Object
Pointer
< Pointer type (T *)
Array
< Array type (T [])
Other
< Any other type, such as delegates, function pointers, struct, void...
struct Box;
Box is a generic container for objects (both value and heap), allowing the user to box them in a generic form and recover them later. A box object contains a value in a generic fashion, allowing it to be passed from one place to another without having to know its type. It is created by calling the box function, and you can recover the value by instantiating the unbox template.
bool unboxable(TypeInfo test);
Return whether this value could be unboxed as the given type without throwing.
const TypeInfo type();
Property for the type contained by the box. This is initially null and cannot be assigned directly.
Returns:
the type of the contained object.
const const(void)[] data();
Property for the data pointer to the value of the box. This is initially null and cannot be assigned directly.
Returns:
the data array.
string toString();
Attempt to convert the boxed value into a string using std.string.format; this will throw if that function cannot handle it. If the box is uninitialized then this returns "".
bool opEquals(Box other);
Compare this box's value with another box. This implicitly casts if the types are different, identical to the regular type system.
float opCmp(Box other);
Compare this box's value with another box. This implicitly casts if the types are different, identical to the regular type system.
hash_t toHash();
Return the value's hash.
Box box(...);
Box the single argument passed to the function. If more or fewer than one argument is passed, this will assert.
Box box(TypeInfo type, void* data);
Box the explicitly-defined object. type must not be null; data must not be null if the type's size is greater than zero. The data is copied.
Box[] boxArray(TypeInfo[] types, void* data);
Convert a list of arguments into a list of boxes.
Box[] boxArray(...);
Box each argument passed to the function, returning an array of boxes.
void boxArrayToArguments(Box[] arguments, out TypeInfo[] types, out void* data);
Convert an array of boxes into an array of arguments.
class UnboxException: object.Exception;
This class is thrown if unbox is unable to cast the value into the desired result.
Box object;
This is the box that the user attempted to unbox.
TypeInfo outputType;
This is the type that the user attempted to unbox the value as.
this(Box object, TypeInfo outputType);
Assign parameters and create the message in the form "Could not unbox from type ... to ... ."
T unboxCastReal(T)(Box value);
A generic unboxer for the real numeric types.
T unboxCastInteger(T)(Box value);
A generic unboxer for the integral numeric types.
T unboxCastComplex(T)(Box value);
A generic unboxer for the complex numeric types.
T unboxCastImaginary(T)(Box value);
A generic unboxer for the imaginary numeric types.
T unbox(T)(Box value);
The unbox template takes a type parameter and returns a function that takes a box object and returns the specified type.
To use it, instantiate the template with the desired result type, and then call the function with the box to convert. This will implicitly cast base types as necessary and in a way consistent with static types - for example, it will cast a boxed byte into int, but it won't cast a boxed float into short.
Throws:
UnboxException if it cannot cast
Example:
Box b = box(4.5);
bit u = unboxable!(real)(b); // This is true.
real r = unbox!(real)(b);
Box y = box(4);
int x = unbox!(int) (y);
bool unboxable(T)(Box value);
Return whether the value can be unboxed as the given type; if this returns false, attempting to do so will throw UnboxException.
分享到:
相关推荐
python学习资源
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
基于Andorid的音乐播放器项目设计(国外开源)实现源码,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
python学习资源
python学习资源
python学习一些项目和资源
【毕业设计】java-springboot+vue家具销售平台实现源码(完整前后端+mysql+说明文档+LunW).zip
HTML+CSS+JavaScarip开发的前端网页源代码
python学习资源
【毕业设计】java-springboot-vue健身房信息管理系统源码(完整前后端+mysql+说明文档+LunW).zip
成绩管理系统C/Go。大学生期末小作业,指针实现,C语言版本(ANSI C)和Go语言版本
1_基于大数据的智能菜品个性化推荐与点餐系统的设计与实现.docx
【毕业设计】java-springboot-vue交流互动平台实现源码(完整前后端+mysql+说明文档+LunW).zip
内容概要:本文主要探讨了在高并发情况下如何设计并优化火车票秒杀系统,确保系统的高性能与稳定性。通过对比分析三种库存管理模式(下单减库存、支付减库存、预扣库存),强调了预扣库存结合本地缓存及远程Redis统一库存的优势,同时介绍了如何利用Nginx的加权轮询策略、MQ消息队列异步处理等方式降低系统压力,保障交易完整性和数据一致性,防止超卖现象。 适用人群:具有一定互联网应用开发经验的研发人员和技术管理人员。 使用场景及目标:适用于电商、票务等行业需要处理大量瞬时并发请求的业务场景。其目标在于通过合理的架构规划,实现在高峰期保持平台的稳定运行,保证用户体验的同时最大化销售额。 其他说明:文中提及的技术细节如Epoll I/O多路复用模型以及分布式系统中的容错措施等内容,对于深入理解大规模并发系统的构建有着重要指导意义。
基于 OpenCV 和 PyTorch 的深度车牌识别
【毕业设计-java】springboot-vue教学资料管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
此数据集包含有关出租车行程的详细信息,包括乘客人数、行程距离、付款类型、车费金额和行程时长。它可用于各种数据分析和机器学习应用程序,例如票价预测和乘车模式分析。
把代码放到Word中,通过开发工具——Visual Basic——插入模块,粘贴在里在,把在硅基流动中申请的API放到VBA代码中。在Word中,选择一个问题,运行这个DeepSeekV3的宏就可以实现在线问答
【毕业设计】java-springboot+vue机动车号牌管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
【毕业设计】java-springboot-vue交通管理在线服务系统的开发源码(完整前后端+mysql+说明文档+LunW).zip