typename{...}(...)(注意{}部分)是内部构造函数"?explicit inner constructor?methods用于检查方法是否是内部/外部构造?顺便说一句,我知道如何使用以及何时使用内部构造函数.我知道内部构造函数是什么,直到外部构造者进入并混淆了水域.:(
让我们回顾一下来自该文档的一些陈述:
1.外部构造方法
构造函数与Julia中的任何其他函数一样,其整体行为由其方法的组合行为定义.
2.内部构造方法
内部构造函数方法很像外部构造函数方法,有两个不同之处:1.它在类型声明的块中声明,而不是像普通方法一样在它之外声明.2.它可以访问一个特殊的本地存在的函数
new,该函数创建块类型的对象.3.参数化构造函数
如果没有任何显式提供的内部构造函数,复合类型的声明会
Point{T<:Real}自动Point{T}为每个可能的类型提供一个内部构造函数T<:Real,其行为就像非参数默认内部构造函数一样.它还提供了一个总的外点构造函数,对真正的参数,它必须是同一类型的.
我发现inner constructor methods不能直接观察methods,甚至是methods(Foo{Int})作品,它实际上并不"像任何其他功能一样",常见的通用功能不能methods以这种方式编辑.
julia> struct Foo{T}
x::T
end
julia> methods(Foo)
# 2 methods for generic function "(::Type)":
(::Type{Foo})(x::T) where T in Main at REPL[1]:2 # outer ctor ?1?
(::Type{T})(arg) where T in Base at sysimg.jl:24 …Run Code Online (Sandbox Code Playgroud) 这是一个简化的例子:
odot(A::Matrix, B::Matrix) = A * B
# using unicode alias ?
odot(A::Matrix, B::Vector) = reshape(A ? reshape(B,size(A)), length(B))
? = odot
julia> @code_warntype rand(3,3) ? rand(9)
Variables:
#self#::#odot
A::Array{Float64,2}
B::Array{Float64,1}
Body:
begin
return (Main.reshape)((A::Array{Float64,2} ? $(Expr(:invoke, LambdaInfo for reshape(::Array{Float64,1}, ::Tuple{Int64,Int64}), :(Main.reshape), :(B), :((Core.tuple)((Base.arraysize)(A,1)::Int64,(Base.arraysize)(A,2)::Int64)::Tuple{Int64,Int64}))))::Any,(Base.arraylen)(B::Array{Float64,1})::Int64)::Any
end::Any
# using odot
odot(A::Matrix, B::Vector) = reshape(odot(A,reshape(B,size(A))), length(B))
julia> @code_warntype rand(3,3) ? rand(9)
Variables:
#self#::#odot
A::Array{Float64,2}
B::Array{Float64,1}
TS::Type{Float64}
Body:
begin
SSAValue(0) = $(Expr(:invoke, LambdaInfo for reshape(::Array{Float64,1}, ::Tuple{Int64,Int64}), :(Main.reshape), :(B), :((Core.tuple)((Base.arraysize)(A,1)::Int64,(Base.arraysize)(A,2)::Int64)::Tuple{Int64,Int64})))
# meta: location REPL[1] odot …Run Code Online (Sandbox Code Playgroud) 我对使用where语法如何使用julia 0.6中的抽象类型限制参数类型的类型参数表示怀疑.
考虑一个示例,我想创建一个采用整数的参数化抽象类型,并定义从中继承的结构.如果我尝试:
abstract type AbstractFoo{T} where T<: Integer end
Run Code Online (Sandbox Code Playgroud)
它失败了,但我可以使用非where语法
abstract type AbstractFoo{T<:Integer} end
Run Code Online (Sandbox Code Playgroud)
鉴于此,我如何实现我的子类型
mutable struct Foo{T} <: AbstractFoo{T} where T <: Integer
bar::T
end
Run Code Online (Sandbox Code Playgroud)
也失败了(Invalid subtyping).我可以where再次绕过语法
mutable struct Foo{T<:Integer} <: AbstractFoo{T}
bar::T
end
Run Code Online (Sandbox Code Playgroud)
但这似乎是多余的(因为T已经被限制为整数).我可以把它留下来吗?:
mutable struct Foo{T} <: AbstractFoo{T}
bar::T
end
Run Code Online (Sandbox Code Playgroud)
最后,随着内部构造函数语法的弃用,有没有办法将内部构造函数定义为:
mutable struct Foo{T} <: AbstractFoo{T}
bar::T
Foo{T}(x::T) where T = new(x)
end
Run Code Online (Sandbox Code Playgroud)
这使得Foo(3)不可能 - 要求我使用Foo{Int}(3).这是故意还是有更好的解决方法?编辑:我想对于内部构造函数问题,我总是可以定义一个外部构造函数Foo(x::T) where {T} = Foo{T}(x).
是否有一个内置的朱莉娅功能剥离LineNumberNode的Expr?特别是对于macrocalls:
julia> ex = :(@foo 1)
:(#= REPL[5]:1 =# @foo 1)
julia> dump(ex)
Expr
head: Symbol macrocall
args: Array{Any}((3,))
1: Symbol @foo
2: LineNumberNode
line: Int64 1
file: Symbol REPL[5]
3: Int64 1
Run Code Online (Sandbox Code Playgroud)
试过MacroTools.striplines,但是
julia> ex = :(@foo 1+1)
:(#= REPL[7]:1 =# @foo 1 + 1)
julia> MacroTools.striplines(ex) |> dump
Expr
head: Symbol macrocall
args: Array{Any}((3,))
1: Symbol @foo
2: LineNumberNode
line: Int64 1
file: Symbol REPL[7]
3: Expr
head: Symbol call
args: Array{Any}((3,)) …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Hugo 中使用 react.js。我知道可以在 HTML 文件中访问 Go 模板变量。
我的问题是如何在 javascript 中访问它们。或者有解决方法吗?
提前致谢。
更新?
目前我的解决方法是meta在 HTML 中使用标签并加载 Go 模板变量,如下所示:
<meta name="title" content={{.Title}} />
Run Code Online (Sandbox Code Playgroud)
然后在javascript中,
function getMetaTitle() {
var metas = document.getElementsByTagName('meta');
for (i=0; i<metas.length; i++) {
if (metas[i].getAttribute("name") == "title") {
return metas[i].getAttribute("content");
}
}
return "failed to access...";
}
var metaTitle = getMetaTitle();
Run Code Online (Sandbox Code Playgroud)
但是当元标签数量增加时这种方式不方便,有没有更简洁的方法来做到这一点?
只需从文档中复制:
pointer_from_objref(object_instance):
生成的Ptr的存在不会保护对象不被垃圾收集,因此您必须确保在Ptr将被使用的整个时间内对象保持引用.
参考文献{T}:
安全引用类型T数据的对象.此类型保证指向正确类型的有效Julia分配内存.只要引用了Ref本身,就可以保护底层数据不被垃圾收集器释放.
当作为ccall参数传递时(作为Ptr或Ref类型),Ref对象将转换为指向它引用的数据的本机指针.
没有无效(NULL)参考.
我想传递一个指向ac函数的指针.根据文档,似乎使用pointer_from_objref并不总是安全的,所以我尝试使用Ref:
# test 1
bufferID = convert(GLuint, 0)
glGenBuffers(1, pointer_from_objref(bufferID))
@show bufferID
out => bufferID = 0x00000001 # ok
# test 2
bufferID = convert(GLuint, 0)
glGenBuffers(1, Ref(bufferID))
@show bufferID
out => bufferID = 0x00000000 # unexpected result
# test 3
bufferID = GLuint[0]
glGenBuffers(1, Ref(bufferID))
@show bufferID[]
out => bufferID[] = 0x00000001 # ok
Run Code Online (Sandbox Code Playgroud)
结果显示,test 2没有任何错误会产生意外结果,但test 3在转换bufferID为数组时工作正常.
我的问题是为什么test 2 …
我想生成一个nx 3矩阵,其中n是像素数(宽度*高度).
x = linspace(-1, 1, width)
y = linspace(-1, 1, height)
r = 1.0
viewDirections = [[i j 1.0] for i in x for j in y]
Run Code Online (Sandbox Code Playgroud)
但是,当我运行这个时,我得到一个:
16-element Array{Array{Float64,2},1}
Run Code Online (Sandbox Code Playgroud)
而不是我想要的16x3 Array{Float64,2}.我显然没有正确地使用理解来构造矩阵.我尝试使用comprehensions来创建一个元组数组,但我不能将这些元组转换为矩阵.
在这里我期望Int类型但得到Float:
julia> function test()
i::Int = 3.0
end
test (generic function with 1 method)
julia> typeof(test())
Float64
Run Code Online (Sandbox Code Playgroud)
在这种情况下,返回类型是Int:
julia> function test()
i::Int = 3.0
i
end
test (generic function with 1 method)
julia> typeof(test())
Int64
Run Code Online (Sandbox Code Playgroud)
这是正确的行为还是错误?
我在Julia中有以下代码行:
X=[(i,i^2) for i in 1:100 if i^2%5==0]
Run Code Online (Sandbox Code Playgroud)
基本上,它返回一个元组列表(i,i^2),从i=1 to 100如果剩余i^2和5为零.我想要做的是,在数组理解中,如果i^2变得大于,则跳出for循环1000.但是,如果我实施
X=[(i,i^2) for i in 1:100 if i^2%5==0 else break end]
Run Code Online (Sandbox Code Playgroud)
我收到错误:syntax: expected "]".
有没有办法轻松打破数组内的for循环?我试过在网上看,但没有出现.