Skip to content

Commit

Permalink
Merge pull request #20418 from JuliaLang/jb/structkeywords
Browse files Browse the repository at this point in the history
use `abstract|primitive type`, `struct`, `mutable struct` for type definitions
  • Loading branch information
JeffBezanson authored Feb 10, 2017
2 parents 78f3c82 + dc84a12 commit 5151271
Show file tree
Hide file tree
Showing 247 changed files with 1,340 additions and 1,286 deletions.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ Language changes
i.e. the case where the type parameters are specified. For clarity, this
definition now must be written as `Foo{T,S}(x) where {T,S<:Real} = new(x)`. ([#11310])

* The keywords used to define types have changed ([#19157], [#20418]).
+ `immutable` changes to `struct`
+ `type` changes to `mutable struct`
+ `abstract` changes to `abstract type ... end`
+ `bitstype 32 Char` changes to `primitive type Char 32 end`
In 0.6, `immutable` and `type` are still allowed as synonyms without a deprecation
warning.

* Multi-line and single-line nonstandard command literals have been added. A
nonstandard command literal is like a nonstandard string literal, but the
syntax uses backquotes (``` ` ```) instead of double quotes, and the
Expand Down
8 changes: 4 additions & 4 deletions base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export Enum, @enum

function basetype end

abstract Enum{T<:Integer}
abstract type Enum{T<:Integer} end

Base.convert{T<:Integer}(::Type{Integer}, x::Enum{T}) = bitcast(T, x)
Base.convert{T<:Integer,T2<:Integer}(::Type{T}, x::Enum{T2}) = convert(T, bitcast(T2, x))
Expand Down Expand Up @@ -45,7 +45,7 @@ julia> f(apple)
"I'm a Fruit with value: 1"
```
`BaseType`, which defaults to `Int32`, must be a bitstype subtype of Integer. Member values can be converted between
`BaseType`, which defaults to `Int32`, must be a primitive subtype of Integer. Member values can be converted between
the enum type and `BaseType`. `read` and `write` perform these conversions automatically.
"""
macro enum(T,syms...)
Expand All @@ -58,7 +58,7 @@ macro enum(T,syms...)
typename = T.args[1]
basetype = eval(current_module(),T.args[2])
if !isa(basetype, DataType) || !(basetype <: Integer) || !isbits(basetype)
throw(ArgumentError("invalid base type for Enum $typename, $T=::$basetype; base type must be an integer bitstype"))
throw(ArgumentError("invalid base type for Enum $typename, $T=::$basetype; base type must be an integer primitive type"))
end
elseif !isa(T,Symbol)
throw(ArgumentError("invalid type expression for enum $T"))
Expand Down Expand Up @@ -103,7 +103,7 @@ macro enum(T,syms...)
end
blk = quote
# enum definition
Base.@__doc__(bitstype $(sizeof(basetype) * 8) $(esc(typename)) <: Enum{$(basetype)})
Base.@__doc__(primitive type $(esc(typename)) <: Enum{$(basetype)} $(sizeof(basetype) * 8) end)
function Base.convert(::Type{$(esc(typename))}, x::Integer)
$(membershiptest(:x, values)) || enum_argument_error($(Expr(:quote, typename)), x)
return bitcast($(esc(typename)), convert($(basetype), x))
Expand Down
32 changes: 16 additions & 16 deletions base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import ..Terminals: raw!, width, height, cmove, getX,

import Base: ensureroom, peek, show, AnyDict

abstract TextInterface
abstract ModeState
abstract type TextInterface end
abstract type ModeState end

export run_interface, Prompt, ModalInterface, transition, reset_state, edit_insert, keymap

immutable ModalInterface <: TextInterface
struct ModalInterface <: TextInterface
modes
end

type MIState
mutable struct MIState
interface::ModalInterface
current_mode
aborted::Bool
Expand All @@ -33,7 +33,7 @@ function show(io::IO, s::MIState)
print(io, "MI State (", s.current_mode, " active)")
end

type Prompt <: TextInterface
mutable struct Prompt <: TextInterface
prompt
# A string or function to be printed before the prompt. May not change the length of the prompt.
# This may be used for changing the color, issuing other terminal escape codes, etc.
Expand All @@ -51,12 +51,12 @@ end

show(io::IO, x::Prompt) = show(io, string("Prompt(\"", x.prompt, "\",...)"))

immutable InputAreaState
struct InputAreaState
num_rows::Int64
curs_row::Int64
end

type PromptState <: ModeState
mutable struct PromptState <: ModeState
terminal
p::Prompt
input_buffer::IOBuffer
Expand All @@ -74,13 +74,13 @@ function input_string_newlines_aftercursor(s::PromptState)
return count(c->(c == '\n'), rest)
end

abstract HistoryProvider
abstract CompletionProvider
abstract type HistoryProvider end
abstract type CompletionProvider end

type EmptyCompletionProvider <: CompletionProvider
mutable struct EmptyCompletionProvider <: CompletionProvider
end

type EmptyHistoryProvider <: HistoryProvider
mutable struct EmptyHistoryProvider <: HistoryProvider
end

reset_state(::EmptyHistoryProvider) = nothing
Expand Down Expand Up @@ -712,7 +712,7 @@ end
# Redirect a key as if `seq` had been the keysequence instead in a lazy fashion.
# This is different from the default eager redirect, which only looks at the current and lower
# layers of the stack.
immutable KeyAlias
struct KeyAlias
seq::String
KeyAlias(seq) = new(normalize_key(seq))
end
Expand Down Expand Up @@ -966,7 +966,7 @@ function write_response_buffer(s::PromptState, data)
refresh_line(s)
end

type SearchState <: ModeState
mutable struct SearchState <: ModeState
terminal
histprompt
#rsearch (true) or ssearch (false)
Expand Down Expand Up @@ -1010,7 +1010,7 @@ function reset_state(s::SearchState)
reset_state(s.histprompt.hp)
end

type HistoryPrompt{T<:HistoryProvider} <: TextInterface
mutable struct HistoryPrompt{T<:HistoryProvider} <: TextInterface
hp::T
complete
keymap_dict::Dict{Char,Any}
Expand All @@ -1020,7 +1020,7 @@ end
HistoryPrompt(hp::T) where T<:HistoryProvider = HistoryPrompt{T}(hp)
init_state(terminal, p::HistoryPrompt) = SearchState(terminal, p, true, IOBuffer(), IOBuffer())

type PrefixSearchState <: ModeState
mutable struct PrefixSearchState <: ModeState
terminal
histprompt
prefix::String
Expand Down Expand Up @@ -1049,7 +1049,7 @@ input_string(s::PrefixSearchState) = String(s.response_buffer)

# a meta-prompt that presents itself as parent_prompt, but which has an independent keymap
# for prefix searching
type PrefixHistoryPrompt{T<:HistoryProvider} <: TextInterface
mutable struct PrefixHistoryPrompt{T<:HistoryProvider} <: TextInterface
hp::T
parent_prompt::Prompt
complete
Expand Down
22 changes: 11 additions & 11 deletions base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ import ..LineEdit:
accept_result,
terminal

abstract AbstractREPL
abstract type AbstractREPL end

answer_color(::AbstractREPL) = ""

const JULIA_PROMPT = "julia> "

type REPLBackend
mutable struct REPLBackend
"channel for AST"
repl_channel::Channel
"channel for results: (value, nothing) or (error, backtrace)"
Expand Down Expand Up @@ -110,7 +110,7 @@ function ip_matches_func(ip, func::Symbol)
return false
end

immutable REPLDisplay{R<:AbstractREPL} <: Display
struct REPLDisplay{R<:AbstractREPL} <: Display
repl::R
end

Expand Down Expand Up @@ -167,7 +167,7 @@ function print_response(errio::IO, val::ANY, bt, show_value::Bool, have_color::B
end

# A reference to a backend
immutable REPLBackendRef
struct REPLBackendRef
repl_channel::Channel
response_channel::Channel
end
Expand All @@ -183,7 +183,7 @@ end

## BasicREPL ##

type BasicREPL <: AbstractREPL
mutable struct BasicREPL <: AbstractREPL
terminal::TextTerminal
waserror::Bool
BasicREPL(t) = new(t,false)
Expand Down Expand Up @@ -241,7 +241,7 @@ end

## LineEditREPL ##

type LineEditREPL <: AbstractREPL
mutable struct LineEditREPL <: AbstractREPL
t::TextTerminal
hascolor::Bool
prompt_color::String
Expand Down Expand Up @@ -275,11 +275,11 @@ LineEditREPL(t::TextTerminal, envcolors = false) = LineEditREPL(t,
Base.text_colors[:yellow],
false, false, false, envcolors)

type REPLCompletionProvider <: CompletionProvider; end
mutable struct REPLCompletionProvider <: CompletionProvider; end

type ShellCompletionProvider <: CompletionProvider; end
mutable struct ShellCompletionProvider <: CompletionProvider; end

immutable LatexCompletions <: CompletionProvider; end
struct LatexCompletions <: CompletionProvider; end

beforecursor(buf::IOBuffer) = String(buf.data[1:buf.ptr-1])

Expand All @@ -306,7 +306,7 @@ function complete_line(c::LatexCompletions, s)
end


type REPLHistoryProvider <: HistoryProvider
mutable struct REPLHistoryProvider <: HistoryProvider
history::Array{String,1}
history_file
start_idx::Int
Expand Down Expand Up @@ -945,7 +945,7 @@ end

## StreamREPL ##

type StreamREPL <: AbstractREPL
mutable struct StreamREPL <: AbstractREPL
stream::IO
prompt_color::String
input_color::String
Expand Down
9 changes: 5 additions & 4 deletions base/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ end

function complete_keyword(s::String)
const sorted_keywords = [
"abstract", "baremodule", "begin", "bitstype", "break", "catch", "ccall",
"abstract type", "baremodule", "begin", "break", "catch", "ccall",
"const", "continue", "do", "else", "elseif", "end", "export", "false",
"finally", "for", "function", "global", "if", "immutable", "import",
"importall", "let", "local", "macro", "module", "quote", "return",
"true", "try", "type", "typealias", "using", "while"]
"finally", "for", "function", "global", "if", "import",
"importall", "let", "local", "macro", "module", "mutable struct",
"primitive type", "quote", "return", "struct",
"true", "try", "typealias", "using", "while"]
r = searchsorted(sorted_keywords, s)
i = first(r)
n = length(sorted_keywords)
Expand Down
8 changes: 4 additions & 4 deletions base/Terminals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Base:

## TextTerminal ##

abstract TextTerminal <: Base.AbstractPipe
abstract type TextTerminal <: Base.AbstractPipe end

# INTERFACE
pipe_reader(::TextTerminal) = error("Unimplemented")
Expand Down Expand Up @@ -89,16 +89,16 @@ disable_bracketed_paste(t::TextTerminal) = nothing

## UnixTerminal ##

abstract UnixTerminal <: TextTerminal
abstract type UnixTerminal <: TextTerminal end

pipe_reader(t::UnixTerminal) = t.in_stream
pipe_writer(t::UnixTerminal) = t.out_stream

type TerminalBuffer <: UnixTerminal
mutable struct TerminalBuffer <: UnixTerminal
out_stream::Base.IO
end

type TTYTerminal <: UnixTerminal
mutable struct TTYTerminal <: UnixTerminal
term_type::String
in_stream::Base.TTY
out_stream::Base.TTY
Expand Down
6 changes: 3 additions & 3 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ end

## Traits for array types ##

abstract LinearIndexing
immutable LinearFast <: LinearIndexing end
immutable LinearSlow <: LinearIndexing end
abstract type LinearIndexing end
struct LinearFast <: LinearIndexing end
struct LinearSlow <: LinearIndexing end

"""
Base.linearindexing(A)
Expand Down
4 changes: 2 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ end

function reinterpret{T,S,N}(::Type{T}, a::Array{S}, dims::NTuple{N,Int})
if !isbits(T)
throw(ArgumentError("cannot reinterpret Array{$(S)} to ::Type{Array{$(T)}}, type $(T) is not a bitstype"))
throw(ArgumentError("cannot reinterpret Array{$(S)} to ::Type{Array{$(T)}}, type $(T) is not a bits type"))
end
if !isbits(S)
throw(ArgumentError("cannot reinterpret Array{$(S)} to ::Type{Array{$(T)}}, type $(S) is not a bitstype"))
throw(ArgumentError("cannot reinterpret Array{$(S)} to ::Type{Array{$(T)}}, type $(S) is not a bits type"))
end
nel = div(length(a)*sizeof(S),sizeof(T))
if prod(dims) != nel
Expand Down
6 changes: 3 additions & 3 deletions base/associative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ function summary(t::Associative)
return string(typeof(t), " with ", n, (n==1 ? " entry" : " entries"))
end

immutable KeyIterator{T<:Associative}
struct KeyIterator{T<:Associative}
dict::T
end
immutable ValueIterator{T<:Associative}
struct ValueIterator{T<:Associative}
dict::T
end

Expand Down Expand Up @@ -290,7 +290,7 @@ and value type and thus its `eltype` is always `Pair{Any,Any}`.
See [`Dict`](@ref) for further help.
"""
type ObjectIdDict <: Associative{Any,Any}
mutable struct ObjectIdDict <: Associative{Any,Any}
ht::Vector{Any}
ndel::Int
ObjectIdDict() = new(Vector{Any}(32), 0)
Expand Down
8 changes: 4 additions & 4 deletions base/asyncmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function asyncmap(f, s::AbstractSparseArray...; kwargs...)
return sparse(asyncmap(f, sa...; kwargs...))
end

type AsyncCollector
mutable struct AsyncCollector
f
results
enumerator::Enumerate
Expand Down Expand Up @@ -302,7 +302,7 @@ function AsyncCollector(f, results, c...; ntasks=0, batch_size=nothing)
AsyncCollector(f, results, enumerate(zip(c...)), ntasks, batch_size)
end

type AsyncCollectorState
mutable struct AsyncCollectorState
chnl::Channel
worker_tasks::Array{Task,1}
enum_state # enumerator state
Expand Down Expand Up @@ -367,15 +367,15 @@ be a function which operates on an array of argument tuples.
`collect(AsyncGenerator(f, c...; ntasks=1))` is equivalent to
`map(f, c...)`.
"""
type AsyncGenerator
mutable struct AsyncGenerator
collector::AsyncCollector
end

function AsyncGenerator(f, c...; ntasks=0)
AsyncGenerator(AsyncCollector(f, Dict{Int,Any}(), c...; ntasks=ntasks))
end

type AsyncGeneratorState
mutable struct AsyncGeneratorState
i::Int
collector_state::AsyncCollectorState
end
Expand Down
4 changes: 2 additions & 2 deletions base/atomics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Holds a reference to an object of type `T`, ensuring that it is only
accessed atomically, i.e. in a thread-safe manner.
Only certain "simple" types can be used atomically, namely the
bitstypes integer and float-point types. These are `Int8`...`Int128`,
primitive integer and float-point types. These are `Int8`...`Int128`,
`UInt8`...`UInt128`, and `Float16`...`Float64`.
New atomic objects can be created from a non-atomic values; if none is
Expand All @@ -61,7 +61,7 @@ julia> x[]
Atomic operations use an `atomic_` prefix, such as `atomic_add!`,
`atomic_xchg!`, etc.
"""
type Atomic{T<:AtomicTypes}
mutable struct Atomic{T<:AtomicTypes}
value::T
Atomic{T}() where T<:AtomicTypes = new(zero(T))
Atomic{T}(value) where T<:AtomicTypes = new(value)
Expand Down
Loading

0 comments on commit 5151271

Please sign in to comment.