diff options
author | awr1 <41453959+awr1@users.noreply.github.com> | 2020-07-07 03:22:55 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-07 10:22:55 +0200 |
commit | 37253d660f93ac86942579defbde790ec557dcf1 (patch) | |
tree | ba57ed88e7a126d5404d9e25e387a3d4debad8b5 /doc | |
parent | 61a6098f2d43e9a2a1e5e57ae24fd15d2b53ffd5 (diff) | |
download | Nim-37253d660f93ac86942579defbde790ec557dcf1.tar.gz |
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
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 ----------------- |