Minor improvements to typecast section of manual (#14896)
* Minor improvements to typecast section of manual * Clarification to casting w/ concrete types * Added less ambiguous language
This commit is contained in:
parent
61a6098f2d
commit
37253d660f
1 changed files with 17 additions and 6 deletions
|
|
@ -3230,7 +3230,7 @@ has lots of advantages:
|
||||||
Type conversions
|
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
|
type name replaces the procedure name. A type conversion is always
|
||||||
safe in the sense that a failure to convert a type to another
|
safe in the sense that a failure to convert a type to another
|
||||||
results in an exception (if it cannot be determined statically).
|
results in an exception (if it cannot be determined statically).
|
||||||
|
|
@ -3265,15 +3265,26 @@ conversions too are now *always unchecked*.
|
||||||
|
|
||||||
Type casts
|
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
|
.. code-block:: nim
|
||||||
cast[int](x)
|
cast[int](x)
|
||||||
|
|
||||||
Type casts are a crude mechanism to interpret the bit pattern of
|
The target type of a cast must be a concrete type, for instance, a target type
|
||||||
an expression as if it would be of another type. Type casts are
|
that is a type class (which is non-concrete) would be invalid:
|
||||||
only needed for low-level programming and are inherently unsafe.
|
|
||||||
|
|
||||||
|
.. 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
|
The addr operator
|
||||||
-----------------
|
-----------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue