mirror of
https://github.com/exaloop/codon.git
synced 2025-06-03 15:03:52 +08:00
* Update docs * Update docs * Update docs * GitBook: [#4] Add hint * Update primer * Re-organize docs * Fix table * Fix link * GitBook: [#5] No subject * GitBook: [#6] No subject * Cleanup and doc fix * Add IR docs * Add ir docs * Fix spelling error * More IR docs * Update README.md * Update README.md * Fix warning * Update intro * Update README.md * Update docs * Fix table * Don't build docs * Update docs * Add Jupyter docs * FIx snippet * Update README.md * Fix images * Fix code block * Update docs, update cmake * Break up tutorial * Update pipeline.svg * Update docs for new version * Add differences with Python docs
58 lines
1.5 KiB
Markdown
58 lines
1.5 KiB
Markdown
Codon supports a number of additional types that are not present
|
|
in plain Python.
|
|
|
|
# Arbitrary-width integers
|
|
|
|
Codon's `int` type is a 64-bit signed integer. However, Codon
|
|
supports arbitrary-width signed and unsigned integers:
|
|
|
|
``` python
|
|
a = Int[16](42) # signed 16-bit integer 42
|
|
b = UInt[128](99) # unsigned 128-bit integer 99
|
|
```
|
|
|
|
The Codon standard library provides shorthands for the common
|
|
variants:
|
|
|
|
- `i8`/`u8`: signed/unsigned 8-bit integer
|
|
- `i16`/`u16`: signed/unsigned 16-bit integer
|
|
- `i32`/`u32`: signed/unsigned 32-bit integer
|
|
- `i64`/`u64`: signed/unsigned 64-bit integer
|
|
|
|
# Pointers
|
|
|
|
Codon has a `Ptr[T]` type that represents a pointer to an object
|
|
of type `T`. Pointers can be useful when interfacing with C. The
|
|
`__ptr__` keyword can also be used to obtain a pointer to a variable:
|
|
|
|
``` python
|
|
p = Ptr[int](100) # allocate a buffer of 100 ints
|
|
p = Ptr[int]() # null pointer
|
|
|
|
x = 42
|
|
p = __ptr__(x) # pointer to x, like "&x" in C
|
|
|
|
from C import foo(Ptr[int])
|
|
foo(p) # pass pointer to C function
|
|
```
|
|
|
|
The `cobj` alias corresponds to `void*` in C and represents a generic
|
|
C or C++ object.
|
|
|
|
{% hint style="warning" %}
|
|
Using pointers directly circumvents any runtime checks, so dereferencing a
|
|
null pointer, for example, will cause a segmentation fault just like in C.
|
|
{% endhint %}
|
|
|
|
# Static arrays
|
|
|
|
The `__array__` keyword can be used to allocate static arrays on the stack:
|
|
|
|
``` python
|
|
def foo(n):
|
|
arr = __array__[int](5) # similar to "long arr[5]" in C
|
|
arr[0] = 11
|
|
arr[1] = arr[0] + 1
|
|
...
|
|
```
|