-
Check function type:
@code_warntype f(2)
: shows input type and types of calculated values including return value, even if you don't annotate type.Base.return_types(f)
: shows return types for each methods if you annotate them, otherwise it showsAny
type@which UnitRange{Int}(3,5)
:@which
macro show where it defined
-
Macro
:- macros work with expressions
at parse time
and cannot access the types of their inputs - returns
expressions
, which is compiled directly rather than requiring a runtimeeval
call.(unlikeEval
objects) - macro's local variables and variables in
expression
would not conflict each other as long as the variable is either the macro's argument name, or declared with aslocal
. macro dispatch
is based on the types of AST that are handed to the macro, not the types thatthe AST evaluates to at runtime
- つまり macro を呼び出す時に与えられたオブジェクトを元に適切な method を呼び出すため、変数など runtime に評価されるオブジェクトを渡すと、評価される前の状態で method を呼び出すことになる(評価後の型を元に dispatch が行われない).
- macros work with expressions
-
Generated functions
:- While macros work with expressions at parse time and cannot access the types of their inputs,
a generated function
gets expanded at a time when thetypes of the arguments are known
, but the function is not yet compiled. - generated functions shouldn't:
- contains side effects(because they might be cached, thus cashing of native pointers also must be avoided)
- interact/observe global mutable state(because definition ordered becomes matter, const global state is ok)
- While macros work with expressions at parse time and cannot access the types of their inputs,
-
Tasks
(also known by several other names, such assymmetric coroutines
,lightweight threads
,cooperative multitasking
, orone-shot continuations
). -
switching tasks does not use any space, so any number of task switches can occur without consuming the call stack.
-
switching among tasks can occur in any order, unlike function calls, where the called function must finish executing before control returns to the calling function.
-
Conventions
: -
Functions that write to their arguments have names that end in
!
. These are sometimes called "mutating
" or "in-place
" functions because they are intended to produce changes in their arguments after the function is called, not just return a value. -
Syntax Conflicts:
Juxtaposed literal coefficient syntax
may conflict with some numeric literal syntaxes: -
In all cases the ambiguity is resolved in favor of interpretation as numeric literals:
-
Expressions starting with
0x/0o/0b
are alwayshexadecimal/octal/binary
literals. -
Expressions starting with a numeric literal followed by
e
orE
are alwaysfloating-point literals
. -
Expressions starting with a numeric literal followed by
f
are always32-bit floating-point literals
. -
Vectorized "dot" operators
:dot calls
:2 .* A.^2 .+ sin.(A)
(or equivalently@. 2A^2 + sin(A)
, using the@. macro
)nested dot calls
likef.(g.(x))
are fused, and "adjacent" binary operators likex .+ 3 .* x.^2
are equivalent to nested dot calls(+).(x, (*).(3, (^).(x, 2)))
.
-
A = [1,2,3]; 0 .< A .< 1;
returns[0,0,0]
, which is a kind of masks for each element in array Av(x) = (println(x); x); v(1) < v(2) <= v(3)
-Operators With Special Names
: A few special expressions correspond to calls to functions with- Expression Calls
[A B C ...]
hcat
- create matix[A; B; C; ...]
vcat
- create matix[A B; C D; ...]
hvcat
- create matix -A'
adjoint
A[i]
getindex
A[i] = x
setindex!
A.n
getproperty
A.n = x
setproperty!
-
Scope constructs
: The constructs introducing scope blocks are:-
Construct
Scope type
Allowed within
-
module, baremodule
global
global
-
struct
local (soft)
global
-
for, while, try
local (soft)
global, local
-
macro
local (hard)
global
-
functions, do blocks, let blocks, comprehensions, generators
local (hard)
global, local
-
those which only introduce a "
soft scope
", which affects whethershadowing
aglobal variable by the same name
is allowed or not. -
begin
block doesn't introduct new scope
-
-
Functions:
- When
a bare identifier
ordot expression
occurs after a semicolon,the keyword argument name is implied by the identifier or field name
. For example, plot(x, y; width)
is equivalent toplot(x, y; width=width)
and,plot(x, y; options.width)
is equivalent toplot(x, y; width=options.width)
- rightmost occurance is applied:
- In the call
plot(x, y; options..., width=2)
it is possible that the options structure also contains a value for width. In such a case the rightmost occurrence takes precedence. Whenplot(x, y; width=3, width=2)
,width=2
is applied
- When
-
Type:
Any
is commonly called "top
" because it is at the apex of the type graph. Julia also has a predefined abstract "bottom
" type, at the nadir of the type graph, which is written asUnion{}
. It is the exact opposite ofAny
: no object is an instance ofUnion{}
and all types are supertypes ofUnion{}
.
-
The type
Vararg{T,N}
corresponds to exactlyN
elements of typeT
.NTuple{N,T}
is a convenient alias forTuple{Vararg{T,N}}
, i.e. a tuple type containing exactlyN
elements of typeT
. -
The syntax
Array{<:Integer}
is a convenient shorthand forArray{T} where T<:Integer
-
Method:
- A definition of one possible behavior for a
function
is called amethod
. - The
choice of which method to execute
when a function is applied is calleddispatch
.
- A definition of one possible behavior for a
-
When is convert called?
: The following language constructs callconvert
:- Assigning to an array converts to the array's element type.
- Assigning to a field of an object converts to the declared type of the field.
- Constructing an object with
new
converts to the object's declared field types. - Assigning to a variable with a declared type (e.g.
local x::T
) converts to that type. - A function with a declared return type converts its return value to that type.
- Passing a value to
ccall
converts it to the corresponding argument type.
-
Modules:
- Due to syntactic ambiguities, qualifying a name that contains only symbols, such as an operator, requires inserting a colon, e.g.
Base.:+
. A small number of operators additionally require parentheses, e.g. Base.:(==)`
- Due to syntactic ambiguities, qualifying a name that contains only symbols, such as an operator, requires inserting a colon, e.g.