How to create an object like `()` in user space - julia

In Julia the empty tuple is both a type and an instance of that type. So isa((),()) is true. Is it possible to create a similar object myself?

I don't believe so. In fact, in Julia 0.4 isa((),()) is no longer true. The type of () is now Tuple{}:
julia> VERSION
v"0.4.0-dev+5441"
julia> typeof(())
Tuple{}
julia> isa((),()) # Throws an error since () is no longer considered a Type
ERROR: TypeError: isa: expected Type{T}, got Tuple{}
I think the only remaining objects that are an instance of themselves are Any, Type and DataType.

Related

MethodError: Cannot `convert` an object of type Array{Float64,1} to an object of type SentinelArrays.ChainedVector{Float64,Array{Float64,1}}

I ran the following loop:
for k in N
Load_dict[k] = fill(0.0,8760)
end
and got the error specified in the title.
Type of N is Array{Symbol,1}, load_dict[k] is SentinelArrays.ChainedVector{Float64,Array{Float64,1}} and fill(0.0, 8760) generates arrays of Array{Float64,1}.
Can Array{Float64,1} be converted to SentinelArrays.ChainedVector{Float64,Array{Float64,1}}? Or is there a way to generate SentinelArrays.ChainedVector{Float64,Array{Float64,1}} arrays, directly?
Thanks!
Currently an appropriate convert method is undefined so you need to use a constructor:
Load_dict[k] = ChainedVector([fill(0.0, 8760)])
However, it seems that the eltype of your Load_dict is overly narrow (unless you really know what you are doing and why you need a collection of ChainedVector).

How to use type using parametric typing

I am trying to use type using parametric typing But I get an Error. My julia version is 0.6.
Code:
type BasicRBC{T <: Real}
a::T
b::T
vce::Matrix{T}
matrix1::Matrix{T}
c::T
output::T
cons::T
vG::Vector{T}
end
Error:
invalid redefinition of constant BasicRBC
a,b,c,output and cons are Float64
matrix1 is Matrix
vG is Array
vce is [ 0.9 0.8 0.1]
You cannot modify a type in the same Julia session because Julia compiles things using the information about the exact type layout in order to make functions fast. Thus if you want to change type definitions, you need to refresh your workspace or restart Julia as #TasosPapastylianou said.– Chris Rackauckas

Julia - Array of UTF8 behavior

I encountered a problem which I've solved, but why the solution works doesnt make sense to me
I had a function similar to this one
function testB(a::Array{AbstractString})
println(a)
end
running it like so gave me
testB(convert(Array{UTF8String},["a","b"]))
ERROR: MethodError: `testB` has no method matching
testB(::Array{UTF8String,1})
Note that Im not manually converting to UTF8 in reality, its for demonstration, in reality I have an AbstractString array, but when I fetch elements from it, they become UFT8
My solution reads in short
function testA{T <: AbstractString}(a::Array{T})
println(a)
end
running this method gives
testA(convert(Array{UTF8String},["a","b"]))
UTF8String["a","b"]
Can anyone tell me why testA works but testB doesnt?
Also, is there a name for this {T <: SomeDataType} notation?
While UTF8String is a subtype of AbstractString, Array{UTF8String} is not a subtype of Array{AbstractString} (no covariance). Hence your testB does not work. (But testB(convert(Array{AbstractString},["a","b"])) should work.)
Rationale for why it has to be like this: a function f(x::Vector{AbstractString}) could e.g. push! a new FooString into x (assuming FooString is a subtype of AbstractString). Now if x was in fact a Vector{UTF8String}, that would fail.

Julia what does a nameless value in the function header mean?

I often see something like the following in Julia:
convert(::Type{Point{Float64}}, ::Float64)
how does the (:: work? And what is the terminology for this?
Your answer can be found in the Julia documentation for defining conversions. To quote (with types switched to make it even more straightforward to read):
The type of the first argument of this method is a singleton
type, Type{Point{Float64}}, the only instance of which is
Point{Float64}. Thus, this method is only invoked when the first
argument is the type value Point{Float64}. Notice the syntax used
for the first argument: the argument name is omitted prior to the ::
symbol, and only the type is given. This is the syntax in Julia for a
function argument whose type is specified but whose value is never
used in the function body. In this example, since the type is a
singleton, there would never be any reason to use its value within the
body.
(Emphasis mine)
You will also encounter the foo(::SomeType) syntax in error messages, when trying to invoke a function with arguments of the wrong type (after all you can't show the argument names of a variant that does not exist). E.g:
julia> foo(x::Bool) = 3
foo (generic function with 1 method)
julia> foo(5)
ERROR: `foo` has no method matching foo(::Int64)

Issue with Julia ccall interface and symbols

I'm attemping to use Julia's ccall function to interface with a C library. All the types and pointers are correct, and the below function call successfully returns the correct answer (variable defintion and setup not shown here for brevity).
ccall((:vDSP_convD, libacc), Void,
(Ptr{T}, Int64, Ptr{T}, Int64, Ptr{T}, Int64, UInt64, UInt64),
x_padded, 1, pointer(K, Ksize), -1, result, 1, Rsize, Ksize)
However, if I wish to generate the function name as a symbol, and then use it as an argument to ccall, it fails.
fname = symbol(string("vDSP_conv", "D"))
ccall((fname , libacc), Void,
(Ptr{T}, Int64, Ptr{T}, Int64, Ptr{T}, Int64, UInt64, UInt64),
x_padded, 1, pointer(K, Ksize), -1, result, 1, Rsize, Ksize)
The error is:
ERROR: LoadError: TypeError: conv: in ccall: first argument not a
pointer or valid constant expression, expected Ptr{T},
got Tuple{Symbol,ASCIIString}
If I print the type of each of these two naming versions, I get
julia> println(typeof(:vDSP_convD))
Symbol
julia> println(typeof(fname))
Symbol
Is there a way to get this to work? I'm guessing that I will have to wrap this in a macro or #eval to get this work, but I am curious as to why the above functionality doesn't work as shown?
Any help will be most appreciated!
EDIT
I ended up wrapping this in an #eval block to get it to work; however, I'm still curious as to the backend logic as to why the above syntax doesn't work (why it interprets a symbol as a pointer sometimes, and then not other times)
ccall is not really a function – it's a syntactic form that is translated to a C function call using the C ABI. To emit a call to a C function, you need to be able to statically resolve the address of the function – that's where this requirement comes from. Note that in both C and Julia you can also call a function using a variable function pointer. In Julia, there are a few ways to get such a pointer, typically by using dlopen and dlsym. What ccall won't do is resolve a function by non-constant name: this is impossible in C (without a building yourself a lookup table); in Julia you can do this, as you've figured out, by using eval – but there is compiler overhead to doing this. That is why ccall won't do this automatically: you don't want to run the risk of accidentally introducing compiler overhead in a loop, for example.

Resources