mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
[zig/en] fix <type>
This commit is contained in:
parent
b5104da2dd
commit
fd80f81d45
@ -7,11 +7,9 @@ contributors:
|
|||||||
|
|
||||||
[Zig][ziglang] aims to be a replacement for the C programming language.
|
[Zig][ziglang] aims to be a replacement for the C programming language.
|
||||||
|
|
||||||
**WARNING**: this document expects you to understand a few basic concepts in computer science, such as pointers, stack and heap memory, etc.
|
**WARNING**: this document expects you to understand a few basic concepts in computer
|
||||||
|
science, such as pointers, stack and heap memory, etc. Prior knowledge of C is
|
||||||
**WARNING**: Zig isn't considered as ready for production. Bugs are expected.
|
recommended.
|
||||||
|
|
||||||
Prior knowledge of C is recommended.
|
|
||||||
|
|
||||||
## Quick overview: Zig compared to C
|
## Quick overview: Zig compared to C
|
||||||
|
|
||||||
@ -20,8 +18,8 @@ Prior knowledge of C is recommended.
|
|||||||
- `try` and `catch` mechanism, which is both convenient, efficient and optional.
|
- `try` and `catch` mechanism, which is both convenient, efficient and optional.
|
||||||
- Most of the C undefined behaviors (UBs) are fixed.
|
- Most of the C undefined behaviors (UBs) are fixed.
|
||||||
- Compared to C, raw pointers are safer to use and less likely to be needed.
|
- Compared to C, raw pointers are safer to use and less likely to be needed.
|
||||||
* The type system distinguishes between a pointer to a single value, or multiple values, etc.
|
- The type system distinguishes between a pointer to a single value, or multiple values, etc.
|
||||||
* Slices are preferred, which is a structure with a pointer and a runtime known size, which characterizes most uses of pointers in the first place.
|
- Slices are preferred, which is a structure with a pointer and a runtime known size, which characterizes most uses of pointers in the first place.
|
||||||
- Some arbitrary language limitations are removed. For example, enumerations, structures and unions can have functions.
|
- Some arbitrary language limitations are removed. For example, enumerations, structures and unions can have functions.
|
||||||
- Simple access to SIMD operations (basic maths on vectors).
|
- Simple access to SIMD operations (basic maths on vectors).
|
||||||
- Zig provides both low-level features of C and the one provided through compiler extensions.
|
- Zig provides both low-level features of C and the one provided through compiler extensions.
|
||||||
@ -218,7 +216,7 @@ if (pointer.* == 1) {
|
|||||||
const foo = pointer.?; // Get the pointed value, otherwise crash.
|
const foo = pointer.?; // Get the pointed value, otherwise crash.
|
||||||
```
|
```
|
||||||
|
|
||||||
### Optional values (?<type>).
|
### Optional values (?\<type\>).
|
||||||
|
|
||||||
```zig
|
```zig
|
||||||
// An optional is a value than can be of any type or null.
|
// An optional is a value than can be of any type or null.
|
||||||
@ -694,6 +692,7 @@ Thus, the standard library lets developers handle memory as they need, through s
|
|||||||
**NOTE**: the choice of the allocator isn't in the scope of this document.
|
**NOTE**: the choice of the allocator isn't in the scope of this document.
|
||||||
A whole book could be written about it.
|
A whole book could be written about it.
|
||||||
However, here are some examples, to get an idea of what you can expect:
|
However, here are some examples, to get an idea of what you can expect:
|
||||||
|
|
||||||
- `page_allocator`.
|
- `page_allocator`.
|
||||||
Allocate a whole page of memory each time we ask for some memory.
|
Allocate a whole page of memory each time we ask for some memory.
|
||||||
Very simple, very dumb, very wasteful.
|
Very simple, very dumb, very wasteful.
|
||||||
@ -924,8 +923,10 @@ test "returns true" {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Compiler built-ins.
|
### Compiler built-ins.
|
||||||
|
|
||||||
The compiler has special functions called "built-ins", starting with an "@".
|
The compiler has special functions called "built-ins", starting with an "@".
|
||||||
There are more than a hundred built-ins, allowing very low-level stuff:
|
There are more than a hundred built-ins, allowing very low-level stuff:
|
||||||
|
|
||||||
- compile-time errors, logging, verifications
|
- compile-time errors, logging, verifications
|
||||||
- type coercion and conversion, even in an unsafe way
|
- type coercion and conversion, even in an unsafe way
|
||||||
- alignment management
|
- alignment management
|
||||||
@ -968,7 +969,6 @@ if (@enumToInt(Value.blah) == 2) { ... }
|
|||||||
Example: is this a single value or an array, is the length known, etc.
|
Example: is this a single value or an array, is the length known, etc.
|
||||||
- Structures need a value for their attributes, and it is still possible to give an undefined value (stack garbage), but at least it is explicitly undefined.
|
- Structures need a value for their attributes, and it is still possible to give an undefined value (stack garbage), but at least it is explicitly undefined.
|
||||||
|
|
||||||
|
|
||||||
## Further Reading
|
## Further Reading
|
||||||
|
|
||||||
For a start, some concepts are presented on [zig.guide][zigguide].
|
For a start, some concepts are presented on [zig.guide][zigguide].
|
||||||
|
Loading…
Reference in New Issue
Block a user