!WTvnrBXAWIqKuxyqrR:matrix.org

Julia (lang)

315 Members
General help and discussion about the Julia language37 Servers

Load older messages


SenderMessageTime
27 Dec 2023
@malte:maltee.de@malte:maltee.deHas anyone ever suggested to bridge the different channels? I know slack and discord can be bridged to matrix. Having multiple channels on multiple platforms seems quite inefficient.14:55:05
28 Dec 2023
@hasnep:matrix.orgHannesI know the Julia Slack can be strict about not archiving the messages on there in any way because they like the ephemeral disappearing messages, so they'd probably be against a bridge01:46:30
@dionisos:matrix.orgdionisos

Hi, I am trying to pass a type as an argument of a function, which create other function, using this type (to avoid type instability). But I get this error:

ERROR: LoadError: syntax: local variable NodeType cannot be used in closure declaration

17:29:43
@dionisos:matrix.orgdionisos *

Hi, I am trying to pass a type as an argument of a function, which create other functions, using this type (to avoid type instability). But I get this error:

ERROR: LoadError: syntax: local variable NodeType cannot be used in closure declaration

18:02:19
@dionisos:matrix.orgdionisos
function create_function(Type)
	function test(a::Type, b::Type)
		return a+b
	end
	return test
end

test = create_function(Int)
18:03:41
@dionisos:matrix.orgdionisos

ERROR: LoadError: syntax: local variable Type cannot be used in closure declaration

18:04:07
@dionisos:matrix.orgdionisos *
function create_function(MyType)
	function test(a::MyType, b::MyType)
		return a+b
	end
	return test
end

test = create_function(Int)
18:04:33
@dionisos:matrix.orgdionisos *

ERROR: LoadError: syntax: local variable MyType cannot be used in closure declaration

18:04:38
@dionisos:matrix.orgdionisosIn fact it is obvious it doesn’t work, given that MyType is given at runtime. But I suppose there is some other ways to avoid type instability in these kinds of cases.18:07:23
@dionisos:matrix.orgdionisos *

Hi, I am trying to pass a type as an argument to a function, which create other functions, using this type (to avoid type instability). But I get this error:

ERROR: LoadError: syntax: local variable NodeType cannot be used in closure declaration

18:39:38
@qwjyh:matrix.orgqwjyh

If the type need only one function, then you can use

function (v::MyType)(a::MyType, b::MyType)
    ...
end

https://github.com/FluxML/Flux.jl/blob/df468ba0a10d86b660eebed98d1b484cbce113a7/src/layers/basic.jl#L170

If the type need two or more functions, define a new method with the type on the first argument. e.g. https://github.com/JuliaStats/Distributions.jl/blob/b2d765246e0f35473fe354dc578a3f7edf11958d/src/univariate/continuous/uniform.jl#L74

19:43:03
@qwjyh:matrix.orgqwjyh *

If the type need only one function, then you can use

function (v::MyType)(a::MyType, b::MyType)
    ...
end

https://github.com/FluxML/Flux.jl/blob/df468ba0a10d86b660eebed98d1b484cbce113a7/src/layers/basic.jl#L170

If the type need two or more functions, define a new method with the type on the first argument like Base.parse.

19:51:01
@qwjyh:matrix.orgqwjyh* If the type need only one function, then you can use function (v::MyType)(a::MyType, b::MyType) ... end https://github.com/FluxML/Flux.jl/blob/df468ba0a10d86b660eebed98d1b484cbce113a7/src/layers/basic.jl#L170 If the type need two or more functions, it is common to define a new method with the type on the first argument like Base.parse. 22:55:07
@dionisos:matrix.orgdionisos
In reply to @qwjyh:matrix.org
If the type need only one function, then you can use
function (v::MyType)(a::MyType, b::MyType)
...
end
https://github.com/FluxML/Flux.jl/blob/df468ba0a10d86b660eebed98d1b484cbce113a7/src/layers/basic.jl#L170
If the type need two or more functions, it is common to define a new method with the type on the first argument like Base.parse.
Thank you for your answer
23:29:20
@dionisos:matrix.orgdionisosI admit I doesn’t know the syntax you are using, I will try to understand how it works23:30:01
@dionisos:matrix.orgdionisosI mean the "(v::MyType)"23:30:14
@dionisos:matrix.orgdionisos

ERROR: LoadError: syntax: Global method definition around test.jl:2 needs to be placed at the top level, or use "eval".

23:57:18
@dionisos:matrix.orgdionisos

When I try like that :

function create_function(MyType)
	function (test::MyType)(a::MyType, b::MyType)
		return a+b
	end
	return test
end
23:58:20
29 Dec 2023
@dionisos:matrix.orgdionisos

Ok I found how to do that, it should be :

function create_function(MyType, value)
	function test(a::MyType, b::MyType) where MyType
		return a+b
	end
	return test
end

But I admit I don’t really understand why.

00:04:23
@dionisos:matrix.orgdionisosAlso I am curious about the "(test::MyType)" syntax00:05:41
@qwjyh:matrix.orgqwjyhDo you want to generate a function based on a type automatically? If normal multiple dispatch doesn't meet your need, you should use macro.00:06:12
@dionisos:matrix.orgdionisos *

Ok I found how to do that, it should be :

function create_function(MyType)
	function test(a::MyType, b::MyType) where MyType
		return a+b
	end
	return test
end

But I admit I don’t really understand why.

00:07:15
@dionisos:matrix.orgdionisos
In reply to @qwjyh:matrix.org
Do you want to generate a function based on a type automatically? If normal multiple dispatch doesn't meet your need, you should use macro.
Yes this is what I want
00:09:11
@dionisos:matrix.orgdionisosOh, I just realized the "where MyType", just make MyType a variable00:10:03
@qwjyh:matrix.orgqwjyhSimple example for the syntax: ``` julia> struct MyType v::Int64 end julia> function (t::MyType)(a::T, b::T) where {T <: Integer} a + b + t.v end julia> x = MyType(1) MyType(1) julia> x(1, 2) 4 ```00:10:04
@dionisos:matrix.orgdionisos
In reply to @qwjyh:matrix.org
Simple example for the syntax:
```
julia> struct MyType
v::Int64
end

julia> function (t::MyType)(a::T, b::T) where {T <: Integer}
a + b + t.v
end

julia> x = MyType(1)
MyType(1)

julia> x(1, 2)
4
```

My problem is that the function is in a closure, and it gives me the error :

ERROR: LoadError: syntax: local variable MyType cannot be used in closure declaration

00:11:31
@dionisos:matrix.orgdionisosI am very probably misunderstanding something.00:12:22
@qwjyh:matrix.orgqwjyh``` julia> macro genfunc(t) quote function test(a::$t, b::$t) a.v + b.v end end end @genfunc (macro with 1 method) julia> test = @genfunc MyType #83#test (generic function with 1 method) julia> test(x, y) 3 ```00:14:55
@dionisos:matrix.orgdionisosI am wondering if I can do it without a macro, I will do it with a macro if there is not another way, but I feel like there is some hole in my understanding and finding how to do it without a macro will help. 00:18:07
@dionisos:matrix.orgdionisos
In reply to @qwjyh:matrix.org
Simple example for the syntax:
```
julia> struct MyType
v::Int64
end

julia> function (t::MyType)(a::T, b::T) where {T <: Integer}
a + b + t.v
end

julia> x = MyType(1)
MyType(1)

julia> x(1, 2)
4
```
I realize I misunderstood you question, but at least now I understand the syntax you where using, thank you.
00:25:22

Show newer messages


Back to Room ListRoom Version: