diff --git a/doc/src/manual/arrays.md b/doc/src/manual/arrays.md index d2dd09906b16f..2ac46480a4369 100644 --- a/doc/src/manual/arrays.md +++ b/doc/src/manual/arrays.md @@ -513,7 +513,7 @@ iterate over any array type. If you write a custom `AbstractArray` type, you can specify that it has fast linear indexing using ```julia -Base.IndexStyle{T<:MyArray}(::Type{T}) = IndexLinear() +Base.IndexStyle(::Type{<:MyArray}) = IndexLinear() ``` This setting will cause `eachindex` iteration over a `MyArray` to use integers. If you don't @@ -716,7 +716,7 @@ Julia sparse matrices have the type `SparseMatrixCSC{Tv,Ti}`, where `Tv` is the values, and `Ti` is the integer type for storing column pointers and row indices.: ```julia -type SparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv,Ti} +struct SparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv,Ti} m::Int # Number of rows n::Int # Number of columns colptr::Vector{Ti} # Column i is in colptr[i]:(colptr[i+1]-1) diff --git a/doc/src/manual/dates.md b/doc/src/manual/dates.md index 6c4e4a84a40a8..4fbd3b953f97a 100644 --- a/doc/src/manual/dates.md +++ b/doc/src/manual/dates.md @@ -119,8 +119,8 @@ julia> dt2 = Date("2015-01-02",df) You can also use the `dateformat""` string macro. This macro creates the `DateFormat` object once when the macro is expanded and uses the same `DateFormat` object even if a code snippet is run multiple times. ```jldoctest -julia> for i=1:10^5 - Date("2015-01-01",dateformat"y-m-d") +julia> for i = 1:10^5 + Date("2015-01-01", dateformat"y-m-d") end ``` diff --git a/doc/src/manual/documentation.md b/doc/src/manual/documentation.md index 754bca4ca9699..85b4fef5a0e41 100644 --- a/doc/src/manual/documentation.md +++ b/doc/src/manual/documentation.md @@ -267,7 +267,7 @@ instance, rather than just on the type itself. In these cases, you can add a met for your custom type that returns the documentation on a per-instance basis. For instance, ```julia -type MyType +struct MyType value::String end diff --git a/doc/src/manual/embedding.md b/doc/src/manual/embedding.md index 14e8d65e79a2f..64aeee8442202 100644 --- a/doc/src/manual/embedding.md +++ b/doc/src/manual/embedding.md @@ -11,7 +11,7 @@ further language bridges (e.g. calling Julia from Python or C#). We start with a simple C program that initializes Julia and calls some Julia code: -``` +```c #include int main(int argc, char *argv[]) @@ -83,15 +83,15 @@ shared data directory. #### Example -``` +```c #include int main(int argc, char *argv[]) { - jl_init(); - (void)jl_eval_string("println(sqrt(2.0))"); - jl_atexit_hook(0); - return 0; + jl_init(); + (void)jl_eval_string("println(sqrt(2.0))"); + jl_atexit_hook(0); + return 0; } ``` @@ -143,7 +143,7 @@ Julia object. Storing simple data types like `Float64` in this way is called `bo the stored primitive data is called `unboxing`. Our improved sample program that calculates the square root of 2 in Julia and reads back the result in C looks as follows: -``` +```c jl_value_t *ret = jl_eval_string("sqrt(2.0)"); if (jl_typeis(ret, jl_float64_type)) { @@ -163,7 +163,7 @@ is used in the above code snippet. Corresponding `jl_box_...` functions are used to convert the other way: -```julia +```c jl_value_t *a = jl_box_float64(3.0); jl_value_t *b = jl_box_float32(3.0f); jl_value_t *c = jl_box_int32(3); @@ -177,7 +177,7 @@ While `jl_eval_string` allows C to obtain the result of a Julia expression, it d passing arguments computed in C to Julia. For this you will need to invoke Julia functions directly, using `jl_call`: -```julia +```c jl_function_t *func = jl_get_function(jl_base_module, "sqrt"); jl_value_t *argument = jl_box_float64(2.0); jl_value_t *ret = jl_call1(func, argument); @@ -211,7 +211,7 @@ safe to use pointers in between `jl_...` calls. But in order to make sure that v `jl_...` calls, we have to tell Julia that we hold a reference to a Julia value. This can be done using the `JL_GC_PUSH` macros: -``` +```c jl_value_t *ret = jl_eval_string("sqrt(2.0)"); JL_GC_PUSH1(&ret); // Do something with ret @@ -226,7 +226,7 @@ Several Julia values can be pushed at once using the `JL_GC_PUSH2` , `JL_GC_PUSH macros. To push an array of Julia values one can use the `JL_GC_PUSHARGS` macro, which can be used as follows: -``` +```c jl_value_t **args; JL_GC_PUSHARGS(args, 2); // args can now hold 2 `jl_value_t*` objects args[0] = some_value; @@ -239,7 +239,7 @@ The garbage collector also operates under the assumption that it is aware of eve object pointing to a young-generation one. Any time a pointer is updated breaking that assumption, it must be signaled to the collector with the `jl_gc_wb` (write barrier) function like so: -``` +```c jl_value_t *parent = some_old_value, *child = some_young_value; ((some_specific_type*)parent)->field = child; jl_gc_wb(parent, child); @@ -253,7 +253,7 @@ can sometimes invoke garbage collection. The write barrier is also necessary for arrays of pointers when updating their data directly. For example: -``` +```c jl_array_t *some_array = ...; // e.g. a Vector{Any} void **data = (void**)jl_array_data(some_array); jl_value_t *some_value = ...; @@ -286,7 +286,7 @@ struct that contains: To keep things simple, we start with a 1D array. Creating an array containing Float64 elements of length 10 is done by: -```julia +```c jl_value_t* array_type = jl_apply_array_type(jl_float64_type, 1); jl_array_t* x = jl_alloc_array_1d(array_type, 10); ``` @@ -294,7 +294,7 @@ jl_array_t* x = jl_alloc_array_1d(array_type, 10); Alternatively, if you have already allocated the array you can generate a thin wrapper around its data: -``` +```c double *existingArray = (double*)malloc(sizeof(double)*10); jl_array_t *x = jl_ptr_to_array_1d(array_type, existingArray, 10, 0); ``` @@ -305,21 +305,21 @@ referenced. In order to access the data of x, we can use `jl_array_data`: -``` +```c double *xData = (double*)jl_array_data(x); ``` Now we can fill the array: -``` +```c for(size_t i=0; i (s,o...)->(LineEdit.edit_move_up(s) || LineEdit.history_prev(s, LineEdit.mode(s).hist)), - # Down Arrow - "\e[B" => (s,o...)->(LineEdit.edit_move_up(s) || LineEdit.history_next(s, LineEdit.mode(s).hist)) + # Up Arrow + "\e[A" => (s,o...)->(LineEdit.edit_move_up(s) || LineEdit.history_prev(s, LineEdit.mode(s).hist)), + # Down Arrow + "\e[B" => (s,o...)->(LineEdit.edit_move_up(s) || LineEdit.history_next(s, LineEdit.mode(s).hist)) ) function customize_keys(repl) - repl.interface = REPL.setup_interface(repl; extra_repl_keymap = mykeys) + repl.interface = REPL.setup_interface(repl; extra_repl_keymap = mykeys) end atreplinit(customize_keys) diff --git a/doc/src/manual/interfaces.md b/doc/src/manual/interfaces.md index f6ae319302b26..fd7b5cba85501 100644 --- a/doc/src/manual/interfaces.md +++ b/doc/src/manual/interfaces.md @@ -294,11 +294,11 @@ julia> SparseArray{T,N}(::Type{T}, dims::NTuple{N,Int}) = SparseArray{T,N}(Dict{ julia> Base.size(A::SparseArray) = A.dims -julia> Base.similar{T}(A::SparseArray, ::Type{T}, dims::Dims) = SparseArray(T, dims) +julia> Base.similar(A::SparseArray, ::Type{T}, dims::Dims) where {T} = SparseArray(T, dims) -julia> Base.getindex{T,N}(A::SparseArray{T,N}, I::Vararg{Int,N}) = get(A.data, I, zero(T)) +julia> Base.getindex(A::SparseArray{T,N}, I::Vararg{Int,N}) where {T,N} = get(A.data, I, zero(T)) -julia> Base.setindex!{T,N}(A::SparseArray{T,N}, v, I::Vararg{Int,N}) = (A.data[I] = v) +julia> Base.setindex!(A::SparseArray{T,N}, v, I::Vararg{Int,N}) where {T,N} = (A.data[I] = v) ``` Notice that this is an `IndexCartesian` array, so we must manually define [`getindex()`](@ref) and [`setindex!()`](@ref) diff --git a/doc/src/manual/metaprogramming.md b/doc/src/manual/metaprogramming.md index 7f9fc660e592d..f3820d04ba602 100644 --- a/doc/src/manual/metaprogramming.md +++ b/doc/src/manual/metaprogramming.md @@ -474,7 +474,7 @@ I execute at runtime. The argument is: (1, 2, 3) Macros are invoked with the following general syntax: -``` +```julia @name expr1 expr2 ... @name(expr1, expr2, ...) ``` @@ -484,7 +484,7 @@ expressions in the first form, and the lack of whitespace after `@name` in the s two styles should not be mixed. For example, the following syntax is different from the examples above; it passes the tuple `(expr1, expr2, ...)` as one argument to the macro: -``` +```julia @name (expr1, expr2, ...) ``` @@ -661,7 +661,7 @@ with any user variables, and `time` and `println` will refer to the standard lib One problem remains however. Consider the following use of this macro: -``` +```julia module MyModule import Base.@time @@ -675,7 +675,7 @@ Here the user expression `ex` is a call to `time`, but not the same `time` funct uses. It clearly refers to `MyModule.time`. Therefore we must arrange for the code in `ex` to be resolved in the macro call environment. This is done by "escaping" the expression with [`esc()`](@ref): -``` +```julia macro time(ex) ... local val = $(esc(ex)) @@ -1075,7 +1075,7 @@ can be used to index into an array `A` using `A[i]`, instead of `A[x,y,z,...]`. is the following: ```jldoctest sub2ind -julia> function sub2ind_loop{N}(dims::NTuple{N}, I::Integer...) +julia> function sub2ind_loop(dims::NTuple{N}, I::Integer...) where N ind = I[N] - 1 for i = N-1:-1:1 ind = I[i]-1 + dims[i]*ind @@ -1114,7 +1114,7 @@ we use generated functions to manually unroll the loop. The body becomes almost instead of calculating the linear index, we build up an *expression* that calculates the index: ```jldoctest sub2ind_gen -julia> @generated function sub2ind_gen{N}(dims::NTuple{N}, I::Integer...) +julia> @generated function sub2ind_gen(dims::NTuple{N}, I::Integer...) where N ex = :(I[$N] - 1) for i = (N - 1):-1:1 ex = :(I[$i] - 1 + dims[$i] * $ex) @@ -1132,7 +1132,7 @@ julia> sub2ind_gen((3, 5), 1, 2) An easy way to find out is to extract the body into another (regular) function: ```jldoctest sub2ind_gen2 -julia> @generated function sub2ind_gen{N}(dims::NTuple{N}, I::Integer...) +julia> @generated function sub2ind_gen(dims::NTuple{N}, I::Integer...) where N return sub2ind_gen_impl(dims, I...) end sub2ind_gen (generic function with 1 method) diff --git a/doc/src/manual/modules.md b/doc/src/manual/modules.md index 870e3029ea12a..cc8cfe7463447 100644 --- a/doc/src/manual/modules.md +++ b/doc/src/manual/modules.md @@ -321,7 +321,7 @@ Other known potential failure scenarios include: code snippet: ```julia - type UniquedById + mutable struct UniquedById myid::Int let counter = 0 UniquedById() = new(counter += 1) diff --git a/doc/src/manual/networking-and-streams.md b/doc/src/manual/networking-and-streams.md index db4d77564521c..8919f55b60e4f 100644 --- a/doc/src/manual/networking-and-streams.md +++ b/doc/src/manual/networking-and-streams.md @@ -175,7 +175,7 @@ anonymous function on the fly: ```julia julia> open("hello.txt") do f - uppercase(readstring(f)) + uppercase(readstring(f)) end "HELLO AGAIN." ``` @@ -186,11 +186,11 @@ Let's jump right in with a simple example involving TCP sockets. Let's first cre ```julia julia> @async begin - server = listen(2000) - while true - sock = accept(server) - println("Hello World\n") - end + server = listen(2000) + while true + sock = accept(server) + println("Hello World\n") + end end Task (runnable) @0x00007fd31dc11ae0 ``` @@ -252,13 +252,13 @@ To see this, consider the following simple echo server: ```julia julia> @async begin - server = listen(2001) - while true - sock = accept(server) - @async while isopen(sock) - write(sock,readline(sock)) + server = listen(2001) + while true + sock = accept(server) + @async while isopen(sock) + write(sock,readline(sock)) + end end - end end Task (runnable) @0x00007fd31dc12e60 @@ -266,7 +266,7 @@ julia> clientside = connect(2001) TCPSocket(RawFD(28) open, 0 bytes waiting) julia> @async while true - write(STDOUT,readline(clientside)) + write(STDOUT,readline(clientside)) end Task (runnable) @0x00007fd31dc11870 diff --git a/doc/src/manual/parallel-computing.md b/doc/src/manual/parallel-computing.md index ed465297c25c6..d3d84f6a652dd 100644 --- a/doc/src/manual/parallel-computing.md +++ b/doc/src/manual/parallel-computing.md @@ -520,7 +520,7 @@ A channel can be visualized as a pipe, i.e., it has a write end and read end. * Multiple readers in different tasks can read data concurrently via [`take!()`](@ref) calls. * As an example: - ``` + ```julia # Given Channels c1 and c2, c1 = Channel(32) c2 = Channel(32) diff --git a/doc/src/manual/performance-tips.md b/doc/src/manual/performance-tips.md index a0c9ed220dbe0..00f2a313ea640 100644 --- a/doc/src/manual/performance-tips.md +++ b/doc/src/manual/performance-tips.md @@ -359,10 +359,10 @@ However, there are cases where you may need to declare different versions of the for different element types of `a`. You could do it like this: ``` -function myfun{T<:AbstractFloat}(c::MySimpleContainer{Vector{T}}) +function myfun(c::MySimpleContainer{Vector{T}}) where T<:AbstractFloat ... end -function myfun{T<:Integer}(c::MySimpleContainer{Vector{T}}) +function myfun(c::MySimpleContainer{Vector{T}}) where T<:Integer ... end ``` @@ -389,17 +389,17 @@ Note the somewhat surprising fact that `T` doesn't appear in the declaration of that we'll return to in a moment. With this approach, one can write functions such as: ```jldoctest containers2 -julia> function myfunc{T<:Integer, A<:AbstractArray}(c::MyContainer{T,A}) +julia> function myfunc(c::MyContainer{<:Integer, <:AbstractArray}) return c.a[1]+1 end myfunc (generic function with 1 method) -julia> function myfunc{T<:AbstractFloat}(c::MyContainer{T}) +julia> function myfunc(c::MyContainer{<:AbstractFloat}) return c.a[1]+2 end myfunc (generic function with 2 methods) -julia> function myfunc{T<:Integer}(c::MyContainer{T,Vector{T}}) +julia> function myfunc(c::MyContainer{T,Vector{T}}) where T<:Integer return c.a[1]+3 end myfunc (generic function with 3 methods) @@ -509,7 +509,7 @@ function norm(A) if isa(A, Vector) return sqrt(real(dot(A,A))) elseif isa(A, Matrix) - return max(svd(A)[2]) + return maximum(svd(A)[2]) else error("norm: invalid argument") end @@ -520,7 +520,7 @@ This can be written more concisely and efficiently as: ```julia norm(x::Vector) = sqrt(real(dot(x,x))) -norm(A::Matrix) = max(svd(A)[2]) +norm(A::Matrix) = maximum(svd(A)[2]) ``` ## Write "type-stable" functions @@ -681,7 +681,7 @@ one approach is to pass the dimensionality as a parameter, for example through ` ["Value types"](@ref)): ```jldoctest -julia> function array3{N}(fillval, ::Type{Val{N}}) +julia> function array3(fillval, ::Type{Val{N}}) where N fill(fillval, ntuple(d->3, Val{N})) end array3 (generic function with 1 method) @@ -715,7 +715,7 @@ code like the above be used.) An example of correct usage of `Val` would be: ```julia -function filter3{T,N}(A::AbstractArray{T,N}) +function filter3(A::AbstractArray{T,N}) where {T,N} kernel = array3(1, Val{N}) filter(A, kernel) end @@ -804,7 +804,7 @@ copies (perhaps the rest of the code can be easily adapted accordingly). We coul do this in at least four ways (in addition to the recommended call to the built-in [`repmat()`](@ref)): ```julia -function copy_cols{T}(x::Vector{T}) +function copy_cols(x::Vector{T}) where T n = size(x, 1) out = Array{T}(n, n) for i = 1:n @@ -813,7 +813,7 @@ function copy_cols{T}(x::Vector{T}) out end -function copy_rows{T}(x::Vector{T}) +function copy_rows(x::Vector{T}) where T n = size(x, 1) out = Array{T}(n, n) for i = 1:n @@ -822,7 +822,7 @@ function copy_rows{T}(x::Vector{T}) out end -function copy_col_row{T}(x::Vector{T}) +function copy_col_row(x::Vector{T}) where T n = size(x, 1) out = Array{T}(n, n) for col = 1:n, row = 1:n @@ -831,7 +831,7 @@ function copy_col_row{T}(x::Vector{T}) out end -function copy_row_col{T}(x::Vector{T}) +function copy_row_col(x::Vector{T}) where T n = size(x, 1) out = Array{T}(n, n) for row = 1:n, col = 1:n @@ -886,7 +886,7 @@ end with ```julia -function xinc!{T}(ret::AbstractVector{T}, x::T) +function xinc!(ret::AbstractVector{T}, x::T) where T ret[1] = x ret[2] = x+1 ret[3] = x+2 @@ -1261,7 +1261,7 @@ strict IEEE behavior for subnormal numbers. Below is an example where subnormals noticeably impact performance on some hardware: ```julia -function timestep{T}( b::Vector{T}, a::Vector{T}, Δt::T ) +function timestep(b::Vector{T}, a::Vector{T}, Δt::T) where T @assert length(a)==length(b) n = length(b) b[1] = 1 # Boundary condition @@ -1271,7 +1271,7 @@ function timestep{T}( b::Vector{T}, a::Vector{T}, Δt::T ) b[n] = 0 # Boundary condition end -function heatflow{T}( a::Vector{T}, nstep::Integer ) +function heatflow(a::Vector{T}, nstep::Integer) where T b = similar(a) for t=1:div(nstep,2) # Assume nstep is even timestep(b,a,T(0.1)) diff --git a/doc/src/manual/style-guide.md b/doc/src/manual/style-guide.md index 86181bcdc8d82..c4ac88ffc20a6 100644 --- a/doc/src/manual/style-guide.md +++ b/doc/src/manual/style-guide.md @@ -123,7 +123,7 @@ Types such as `Union{Function,AbstractString}` are often a sign that some design When creating a type such as: ```julia -struct MyType +mutable struct MyType ... x::Union{Void,T} end