You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/lecture_04/lecture.md
+55-55Lines changed: 55 additions & 55 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Package development
1
+
# [Package development](@id pkg_lecture)
2
2
3
3
Organization of the code is more important with the increasing size of the project and the number of contributors and users. Moreover, it will become essential when different codebases are expected to be combined and reused.
4
4
- Julia was designed from the beginning to encourage code reuse across different codebases as possible
@@ -32,7 +32,7 @@ export test1
32
32
end
33
33
```
34
34
35
-
Function ```include``` copies content of the file to this location (will be part of the module).
35
+
Function `include` copies content of the file to this location (will be part of the module).
36
36
37
37
Creates functions:
38
38
```julia
@@ -42,13 +42,13 @@ MySpace.test2
42
42
43
43
For easier manipulation, these functions can be "exported" to be exposed to the outer world (another namespace).
44
44
45
-
Keyword: ```using``` exposes the exported functions and structs:
45
+
Keyword: `using` exposes the exported functions and structs:
46
46
```julia
47
47
using.MySpace
48
48
```
49
49
The dot means that the module was defined in this scope.
50
50
51
-
Keyword: ```import``` imports function with availability to redefine it.
51
+
Keyword: `import` imports function with availability to redefine it.
52
52
53
53
Combinations:
54
54
@@ -78,8 +78,8 @@ When importing/using functions with name that that is already imported/used from
78
78
- both function has to be acessed by their full names.
79
79
80
80
Resoluton:
81
-
- It may be easier to cherry pick only the functions we need (rather than importing all via ```using```)
82
-
- remane some function using keyword ```as```
81
+
- It may be easier to cherry pick only the functions we need (rather than importing all via `using`)
82
+
- remane some function using keyword `as`
83
83
```julia
84
84
import MySpace2: test1 as t1
85
85
```
@@ -100,18 +100,18 @@ end;
100
100
```
101
101
102
102
REPL of Julia is a module called "Main".
103
-
- modules are not copied, but referenced, i.e. ```B.b===B.C.c```
103
+
- modules are not copied, but referenced, i.e. `B.b===B.C.c`
104
104
- including one module twice (from different packages) is not a problem
105
105
106
106
### Revise.jl
107
107
108
-
The fact that julia can redefine a function in a Module by importing it is used by package ```Revise.jl``` to synchronize REPL with a module or file.
108
+
The fact that julia can redefine a function in a Module by importing it is used by package `Revise.jl` to synchronize REPL with a module or file.
109
109
110
110
So far, we have worked in REPL. If you have a file that is loaded and you want to modify it, you would need to either:
111
111
1. reload the whole file, or
112
112
2. copy the changes to REPL
113
113
114
-
```Revise.jl``` do the latter automatically.
114
+
`Revise.jl` do the latter automatically.
115
115
116
116
Example demo:
117
117
```julia
@@ -120,8 +120,8 @@ includet("example.jl")
120
120
```
121
121
122
122
Works with:
123
-
- any package loaded with ```import``` or ```using```,
124
-
- script loaded with ```includet```,
123
+
- any package loaded with `import` or `using`,
124
+
- script loaded with `includet`,
125
125
- Base julia itself (with Revise.track(Base))
126
126
- standard libraries (with, e.g., using Unicode; Revise.track(Unicode))
127
127
@@ -148,39 +148,39 @@ Every module introduces a new global scope.
148
148
149
149
- No variable or function is expected to exist outside of it
150
150
- Every module is equal a global scope (no single "global" exists)
151
-
- The REPL is a global module called ```Main```
151
+
- The REPL is a global module called `Main`
152
152
153
153
- Local scope
154
154
155
-
Variables in Julia do not need to be explcitely declared, they are created by assignments: ```x=1```.
155
+
Variables in Julia do not need to be explcitely declared, they are created by assignments: `x=1`.
156
156
157
-
In local scope, the compiler checks if variable ```x``` does not exists outside. We have seen:
157
+
In local scope, the compiler checks if variable `x` does not exists outside. We have seen:
158
158
159
159
```julia
160
160
x=1
161
161
f(y)=x+y
162
162
```
163
163
164
-
The rules for local scope determine how to treat assignment of ```x```. If local ```x``` exists, it is used, if it does not:
165
-
- in *hard* scope: new local ```x``` is created
166
-
- in *soft* scope: checks if ```x``` exists outside (global)
167
-
- if not: new local ```x``` is created
164
+
The rules for local scope determine how to treat assignment of `x`. If local `x` exists, it is used, if it does not:
165
+
- in *hard* scope: new local `x` is created
166
+
- in *soft* scope: checks if `x` exists outside (global)
167
+
- if not: new local `x` is created
168
168
- if yes: the split is REPL/non-interactive:
169
-
- REPL: global ```x``` is used (convenience, as of 1.6)
170
-
- non-interactive: local ```x``` is created
169
+
- REPL: global `x` is used (convenience, as of 1.6)
170
+
- non-interactive: local `x` is created
171
171
172
172
173
-
- keyword ```local``` and ```global``` can be used to specify which variable to use
173
+
- keyword `local` and `global` can be used to specify which variable to use
174
174
175
175
From documentation:
176
176
177
177
| Construct | Scope type | Allowed within |
178
178
|:----------|:-----------|:---------------|
179
-
|[`module`](@ref), [`baremodule`](@ref)| global | global |
180
-
|[`struct`](@ref)| local (soft) | global |
181
-
|[`for`](@ref), [`while`](@ref), [`try`](@ref try)| local (soft) | global, local |
182
-
|[`macro`](@ref)| local (hard) | global |
183
-
| functions, [`do`](@ref) blocks, [`let`](@ref) blocks, comprehensions, generators | local (hard) | global, local |
179
+
|`module`, `baremodule`| global | global |
180
+
|`struct`| local (soft) | global |
181
+
|`for`, `while`, `try`| local (soft) | global, local |
182
+
|`macro`| local (hard) | global |
183
+
| functions, `do` blocks, `let` blocks, comprehensions, generators | local (hard) | global, local |
184
184
185
185
Question:
186
186
```julia
@@ -198,9 +198,9 @@ end
198
198
@show x;
199
199
```
200
200
201
-
## Packages
201
+
## Packages
202
202
203
-
Package is a source tree with a standard layout. Can be loaded with ```include``` or ```using``` and provides a module.
203
+
Package is a source tree with a standard layout. Can be loaded with `include` or `using` and provides a module.
Content of files ```Project.toml``` and ```Manifest.toml``` are maintained by PackageManager.
253
+
Content of files `Project.toml` and `Manifest.toml` are maintained by PackageManager.
254
254
255
255
## Package/Project manager
256
256
257
257
Handles both packages and projects:
258
-
- creating a project ```]generate PkgName```
259
-
- adding an existing project ```add PkgName``` or ``` add https://github.com/JuliaLang/Example.jl```
258
+
- creating a project `]generate PkgName`
259
+
- adding an existing project `add PkgName` or ` add https://github.com/JuliaLang/Example.jl`
260
260
261
261
Names are resolved by Registrators (public or private).
262
262
263
-
- removing ```]rm PkgName```
264
-
- updating ```]update```
265
-
- developing ```]dev http://...```
263
+
- removing `]rm PkgName`
264
+
- updating `]update`
265
+
- developing `]dev http://...`
266
266
267
-
Always reads the actual content of files. ```Add``` creates a precompiled blob.
267
+
Always reads the actual content of files. `Add` creates a precompiled blob.
268
268
269
-
By default these operations are related to environment ```.julia/environments/v1.6```
269
+
By default these operations are related to environment `.julia/environments/v1.6`
270
270
271
-
E.g. running an updating will update packages in ```Manifest.toml``` in this directory. What if the update breaks functionality of some project package that uses special features?
271
+
E.g. running an updating will update packages in `Manifest.toml` in this directory. What if the update breaks functionality of some project package that uses special features?
272
272
273
273
There can be more than one environment!
274
274
275
275
Any package can define its own project environment with a list of dependencies.
276
276
277
-
- switching by ```]activate Path```
277
+
- switching by `]activate Path`
278
278
279
279
- from that moment, all package modifications will be relevant only to this project!
280
-
- when switching to a new project ```]instantiate``` will prepare (download and precompile) the environment
281
-
- which Packages are visible is determined by ```LOAD_PATH```
280
+
- when switching to a new project `]instantiate` will prepare (download and precompile) the environment
281
+
- which Packages are visible is determined by `LOAD_PATH`
282
282
- typically contaings default libraries and default environment
283
283
- it is different for REPL and Pkg.tests ! No default env. in tests.
284
284
285
285
## Unit testing, /test
286
286
287
287
Without explicit keywords for checking constructs (think missing functions in interfaces), the good quality of the code is guaranteed by detailed unit testing.
288
288
289
-
- each package should have directory ```/test```
290
-
- file ```/test/runtest.jl``` is run by command ```]test``` of the package manager
289
+
- each package should have directory `/test`
290
+
- file `/test/runtest.jl` is run by command `]test` of the package manager
291
291
292
-
this file typically contains ```include``` of other tests
292
+
this file typically contains `include` of other tests
293
293
294
294
- no formal structure of tests is prescribed
295
295
- test files are just ordinary julia scripts
296
296
- user is free to choose what to test and how (freedom x formal rules)
297
297
298
-
- testing functionality is supported by macros ```@test``` and ```@teststet```
298
+
- testing functionality is supported by macros `@test` and `@teststet`
299
299
300
300
```
301
301
@testset "trigonometric identities" begin
@@ -317,7 +317,7 @@ Testset is a collection of tests that will be run and summarized in a common rep
317
317
end
318
318
```
319
319
320
-
- Useful macro```≈``` checks for equality with given tolerance
320
+
- Useful macro`≈` checks for equality with given tolerance
321
321
322
322
```julia
323
323
a=5+1e-8
@@ -372,8 +372,8 @@ PackageName/
372
372
```
373
373
374
374
Where the line-by-line documentation is in the source files.
375
-
- ```/docs/src``` folder can contain more detailed information: introductory pages, howtos, tutorials, examples
376
-
- running ```make.jl``` controls which pages are generated in what form (html or latex) documentation in the /build directory
375
+
- `/docs/src` folder can contain more detailed information: introductory pages, howtos, tutorials, examples
376
+
- running `make.jl` controls which pages are generated in what form (html or latex) documentation in the /build directory
377
377
- automated with GitHub actions
378
378
379
379
Documentation is generated by the julia code.
@@ -397,7 +397,7 @@ Documentation is generated by the julia code.
397
397
y =MyType("y")
398
398
```
399
399
400
-
See ```?x``` and ```?y```.
400
+
See `?x` and `?y`.
401
401
402
402
It uses the same very standdr building blocks: multiple dispatch.
403
403
@@ -409,7 +409,7 @@ If it defines methods that extend previously defined (e.g. from Base), it may af
409
409
410
410
Julia has a tracking mechanism that stores information about the whole graph of dependencies.
411
411
412
-
Faster code can be achieved by the ```precompile``` directive:
412
+
Faster code can be achieved by the `precompile` directive:
0 commit comments