Hello, I am new to Julia, I would like to understand language more deeply, so I have several question:
(I am using 1.12.x version)
First of all, where can I find some BNF like definition?
Another question, I tried to create this composite type (struct}
struct OMG2{X<: Number, Y<: String}
a::Int128
b::Int128
function OMG2(x::T) where T<:Float64
println("Float64")
end
function OMG2(x::T, y::U) where {T<: Int64,U <: Int64}
println("Int64, Int64")
end
function OMG2(x::T, y::U) where {T<: Float32,U <: Float32}
println("Float32, Float32")
end
end
I don’t understand why if the constructors are within the struct the var types X, Y are not propagated to constructors:
Also seems that the constructors don’t have access to the var a and b
struct OMG2{X<: Number, Y<: String}
a::Int128
b::Int128
function OMG2(x::Y)
# Wont work <----
println("THIS WONT WORK")
end
function OMG2(x::T) where T<:Float64
println("Any")
end
function OMG2(x::T, y::U) where {T<: Int64,U <: Int64}
println("Int64, Int64")
end
function OMG2(x::T, y::U) where {T<: Float32,U <: Float32}
println("Float32, Float32")
end
end
I would like to have some kind of restriction, in inner constructors, but seems that this restriction is applied only if I have something like that (constructors are defined out of the composite types, and I don’t understand why):
struct OMG3{X<: Number, Y<: String}
a::Int128
b::Int128
function OMG3(x::T) where T<:Float64
println("Float64")
end
end
# Created constructor out of the struct, and I don't understand that the conditions {X<:Number, Y<: String, is applied just for this kind of constructor
julia> OMG3{Int64, String}() = println("Int64, String")
# Seems that it is is basically some kind of instance
julia> OMG3{Int64, String}.
flags hash
instance layout
name parameters
super types
julia> OMG3{Int64, String}.instance
# Constructor in the struct, seems that it is "empty"
julia> OMG3{Float64}.
body
var
# So, I have 2 constructors, but if I do:
julia> methods(OMG3)
# 1 method for type constructor:
[1] OMG3(x::T) where T<:Float64
@ ~/devel/julia/base64proj/base64test.jl:9
# I will see just the struct one, and I don't understand why.
# From one side, I have restrictions based on my <:, and that constructor is not included in the methods output (it is created outside of the struct), and from other side I have struct constructor, but I can change the type restriction and seems, that it doesn't have access to the struct attributes like a, b
I am confused can somebody help me?