Skip to content

Commit

Permalink
improve sizeof and repr documentation (#26926)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored and JeffBezanson committed May 20, 2018
1 parent cf34b82 commit d247ca8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
14 changes: 11 additions & 3 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,11 @@ reinterpret(::Type{Unsigned}, x::Float16) = reinterpret(UInt16,x)
reinterpret(::Type{Signed}, x::Float16) = reinterpret(Int16,x)

"""
sizeof(T)
sizeof(T::DataType)
sizeof(obj)
Size, in bytes, of the canonical binary representation of the given DataType `T`, if any.
Size, in bytes, of the canonical binary representation of the given `DataType` `T`, if any.
Size, in bytes, of object `obj` if it is not `DataType`.
# Examples
```jldoctest
Expand All @@ -364,9 +366,15 @@ julia> sizeof(Float32)
julia> sizeof(ComplexF64)
16
julia> sizeof(1.0)
8
julia> sizeof([1.0:10.0;])
80
```
If `T` does not have a specific size, an error is thrown.
If `DataType` `T` does not have a specific size, an error is thrown.
```jldoctest
julia> sizeof(AbstractArray)
Expand Down
16 changes: 16 additions & 0 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ See also: [`getindex`](@ref), [`checkbounds`](@ref)
## basic generic definitions ##

eltype(::Type{<:AbstractString}) = Char # some string types may use another AbstractChar

"""
sizeof(str::AbstractString)
Size, in bytes, of the string `s`. Equal to the number of code units in `s` multiplied by
the size, in bytes, of one code unit in `s`.
# Examples
```jldoctest
julia> sizeof("")
0
julia> sizeof("∀")
3
```
"""
sizeof(s::AbstractString) = ncodeunits(s) * sizeof(codeunit(s))
firstindex(s::AbstractString) = 1
lastindex(s::AbstractString) = thisind(s, ncodeunits(s))
Expand Down
8 changes: 7 additions & 1 deletion base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ The optional keyword argument `context` can be set to an `IO` or [`IOContext`](@
object whose attributes are used for the I/O stream passed to `show`.
Note that `repr(x)` is usually similar to how the value of `x` would
be entered in Julia. See also [`repr("text/plain", x)`](@ref) to instead
be entered in Julia. See also [`repr(MIME("text/plain"), x)`](@ref) to instead
return a "pretty-printed" version of `x` designed more for human consumption,
equivalent to the REPL display of `x`.
Expand All @@ -182,6 +182,12 @@ julia> repr(1)
julia> repr(zeros(3))
"[0.0, 0.0, 0.0]"
julia> repr(big(1/3))
"3.33333333333333314829616256247390992939472198486328125e-01"
julia> repr(big(1/3), context=:compact => true)
"3.33333e-01"
```
"""
repr(x; context=nothing) = sprint(show, x; context=context)
Expand Down

0 comments on commit d247ca8

Please sign in to comment.