# v = [ (Try with and without commas, which one is right?) # bitstring.(v) # Remember the `.` (dot), pronounced broadcast, applies the function to every element. # v = [ #v = [ # bitstring.(v) bitstring(Float16(1.0)) function vector_of_types(n::Int) # Here :: says the input is defined for Int's [Int64(n), Float64(n), Float16(n), string(n)] end vector_of_types(17) typeof(vector_of_types(17)) struct FourTypes n::Int64 x::Float64 y::Float16 z::String end f = FourTypes(17,17.0,Float16(17),"17") f = FourTypes(17,17,17,17) f = FourTypes(vector_of_types(17)...) dump(f) typeof(f) ## Test your type # FourVectorTypes([1,2,3],[1.0,2,3],Float16.([1,2,3]),["1","2","3","4"]) struct MyTypeAndMe{T} n::T end MyTypeAndMe(17) typeof(MyTypeAndMe(17)) typeof(MyTypeAndMe(17.0)) typeof(MyTypeAndMe(Float16(17))) typeof(MyTypeAndMe("17")) s = MyTypeAndMe(42) s.n dump(s) typeof(s) s = MyTypeAndMe(rand(5)) s.n dump(s) typeof(s) struct ExtendSqrt2{T} a::T b::T end Base.:show(io::IO, x::ExtendSqrt2) = print(io, "$(x.a)+$(x.b)√2") ExtendSqrt2(3,4) typeof(ExtendSqrt2(3,4)) ExtendSqrt2(3.5,4.1) typeof(ExtendSqrt2(3.5,4.1)) ExtendSqrt2(1//3,1//2) typeof(ExtendSqrt2(1//3,1//2)) Base.:*(x::ExtendSqrt2,y::ExtendSqrt2)=ExtendSqrt2(x.a*y.a+2x.b*y.b,x.a*y.b+y.a*x.b) Base.:+(x::ExtendSqrt2,y::ExtendSqrt2)=ExtendSqrt2(x.a+y.a,x.b+y.b) [ExtendSqrt2(i,j) for i=1:3,j=1:3]^2 s = :abc typeof(s) MeAndMyType(:abc) abstract type HW2 end # This creates an abstract type called HW2 so we don't mix things up struct Plus{A, B} <: HW2 a::A b::B end Base.:+(x::Symbol, y::Symbol) = Plus(x,y) Base.:+(x::HW2, y::Symbol) = Plus(x,y) :a + :b :a + :b + :c :a + :b + :c + :d abstract type Op end struct Add{A, B} <: Op a::A b::B end struct Subtract{A, B} <: Op a::A b::B end struct Max{A, B} <: Op a::A b::B end struct Mul{A,B} <: Op a::A b::B end struct LazyVar <: Op s :: Symbol end Base.:show(io::IO, format::MIME"text/html", x::LazyVar) = print(io,"", x.s, "") macro var(v) esc(:($v = $(LazyVar(v)))) end function evaluate(x::Add; vals...) evaluate(x.a; vals...) + evaluate(x.b; vals...) end Base.:+(x::Op, y::Op) = Add(x,y) Base.:+(x::Op, y::Number) = Add(x,y) Base.:+(x::Number, y::Op) = Add(x,y) #sub function evaluate(x::Subtract; vals...) evaluate(x.a; vals...) - evaluate(x.b; vals...) end Base.:-(x::Op, y::Op) = Subtract(x,y) #max function evaluate(x::Max; vals...) max(evaluate(x.a; vals...), evaluate(x.b; vals...)) end Base.:max(x::Op, y::Op) = Max(x,y) #mul function evaluate(x::Mul; vals...) evaluate(x.a; vals...) * evaluate(x.b; vals...) end Base.:*(x::Op, y::Op ) = Mul(x,y) Base.:*(x::Number,y::Op) = Mul(x,y) Base.:*(x::Op,y::Number) = Mul(x,y) evaluate(x::LazyVar; vals...) = vals.data[x.s] evaluate(x; vals...) = x @var x @var y u = x + 3*y v = max(u,10*y) w = y*x*u for t∈[u,v,w] println(evaluate(t,x=5,y=4)) end