DISCLAIMER. This may not necessarily be an answer to your question. But I wanted to post it as an answer, as a comment does not allow me to express the below that easily.
mutable struct Blockchain
chain::Array{Dict{String, Any}}
current_transactions::Array{String}
block::Dict{String, Any}
new_block::Function
function Blockchain(chain::Array{Dict{String, Any}}, current::Array{String})
new(chain, current, new_block(previous_hash=1, proof=100))
# ^issue calling function within the constructor here
end
end
Here, I assume that you are trying to add some member function functionality to your struct, as you have already stated that you are coming from a C++ background. However, this is not Julian. In Julia, as @crstnbr has already suggested, we need to define global functions that act on objects. The convention is that you add ! at the end of the function to indicate that the function will be changing at least one of its arguments.
Then, by checking the definition of your new_block:
function new_block(proof::String, previous_hash::String=nothing)
block = Dict(
"index" => length(chain) + 1, # which chain?
"timestamp" => time(),
"transactions" => current_transactions, # which current_transactions?
"proof" => proof,
"previous_hash" => previous_hash | hash(chain[end]), # which chain?
)
current_transactions = []
append!(chain, block) # which chain?
return block
end
I notice a couple of serious mistakes. First, there are some undefined variables that you try to use such as chain and current_transactions. I assume, again from C++, you have thought that new_block would be a member function of Blockchain, and hence, could see its chain member variable. This is not how Julia works. Second problem is how you try to call new_block:
new_block(previous_hash=1, proof=100)
This call is totally wrong. The above call notation relies on keyword arguments; however, your function definition only has positional arguments. To be able to support keyword arguments, you need to change your function definition to read as below:
function new_block(; proof::String, previous_hash::String=nothing)
# ^ note the semi-colon here
# ...
end
And last, you define proof and previous_hash to be of type String, but call them with 1, 100and nothing, which are of type Int, Int and Void.
I could not understand your design choice for the Blockchain application in your mind, but I strongly suggest that you should go step-by-step with simpler examples to learn the language. For instance, if you just try the below examples, you will understand how type annotations work in Julia:
Main> f(s::String = nothing) = s
f (generic function with 2 methods)
Main> f()
ERROR: MethodError: no method matching f(::Void)
Closest candidates are:
f(::String) at none:1
f() at none:1
Stacktrace:
[1] f() at ./none:1
[2] eval(::Module, ::Any) at ./boot.jl:235
Main> g(s::String) = s
g (generic function with 1 method)
Main> g(100)
ERROR: MethodError: no method matching g(::Int64)
Closest candidates are:
g(::String) at none:1
Stacktrace:
[1] eval(::Module, ::Any) at ./boot.jl:235
Main> h1(var1 = 1, var2 = 100) = var1 + var2
h1 (generic function with 3 methods)
Main> h1(var2 = 5, var1 = 6)
ERROR: function h1 does not accept keyword arguments
Stacktrace:
[1] kwfunc(::Any) at ./boot.jl:237
[2] eval(::Module, ::Any) at ./boot.jl:235
One last comment is that as far as I can see from your example, you do not need mutable struct. struct should simply help you with your design --- you can still add to/modify its chain, current_transactions and block variables. Check, again, the simpler example below:
Main> struct MyType
a::Vector{Float64}
end
Main> m = MyType([1,2,3]);
Main> append!(m.a, 4);
Main> m
MyType([1.0, 2.0, 3.0, 4.0])
You can think of MyType's a variable, in the above example, as being double * const a in C++terms. You are not allowed to change a to point to a different memory location, but you can modify the memory location pointed-to by a.
In short, you should definitely try to learn the language from the official documentation step-by-step, and post questions here involving really minimal examples. Your example is convoluted in that sense.