diff options
Diffstat (limited to 'doc')
-rw-r--r-- | doc/manual.rst | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/doc/manual.rst b/doc/manual.rst index 99888ad53..c5608da84 100644 --- a/doc/manual.rst +++ b/doc/manual.rst @@ -3230,7 +3230,7 @@ has lots of advantages: Type conversions ---------------- -Syntactically a `type conversion` is like a procedure call, but a +Syntactically a *type conversion* is like a procedure call, but a type name replaces the procedure name. A type conversion is always safe in the sense that a failure to convert a type to another results in an exception (if it cannot be determined statically). @@ -3265,15 +3265,26 @@ conversions too are now *always unchecked*. Type casts ---------- -Example: + +*Type casts* are a crude mechanism to interpret the bit pattern of an expression +as if it would be of another type. Type casts are only needed for low-level +programming and are inherently unsafe. .. code-block:: nim cast[int](x) - -Type casts are a crude mechanism to interpret the bit pattern of -an expression as if it would be of another type. Type casts are -only needed for low-level programming and are inherently unsafe. - + +The target type of a cast must be a concrete type, for instance, a target type +that is a type class (which is non-concrete) would be invalid: + +.. code-block:: nim + type Foo = int or float + var x = cast[Foo](1) # Error: cannot cast to a non concrete type: 'Foo' + +Type casts should not be confused with *type conversions,* as mentioned in the +prior section. Unlike type conversions, a type cast cannot change the underlying +bit pattern of the data being casted (aside from that the size of the target type +may differ from the source type). Casting resembles *type punning* in other +languages or C++'s ``reinterpret_cast`` and ``bit_cast`` features. The addr operator ----------------- |