!WTvnrBXAWIqKuxyqrR:matrix.org

Julia (lang)

320 Members
General help and discussion about the Julia language38 Servers

Load older messages


SenderMessageTime
29 Dec 2023
@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
@dionisos:matrix.orgdionisosOk after some testes and research, it seems like I should use a macro for this.00:27:20
@dionisos:matrix.orgdionisosThank you for you help qwjyh00:27:34
@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 your question, but at least now I understand the syntax you where using, thank you.
00:27:44
30 Dec 2023
@tuxtuxtuxtuxtux:opensuse.org@tuxtuxtuxtuxtux:opensuse.org set a profile picture.14:54:13
@tuxtuxtuxtuxtux:opensuse.org@tuxtuxtuxtuxtux:opensuse.org removed their profile picture.14:54:31
@tuxtuxtuxtuxtux:opensuse.org@tuxtuxtuxtuxtux:opensuse.org set a profile picture.14:56:09
@tuxtuxtuxtuxtux:opensuse.org@tuxtuxtuxtuxtux:opensuse.org removed their profile picture.14:57:05
31 Dec 2023
@jyxu:mozilla.org@jyxu:mozilla.org joined the room.08:32:28
1 Jan 2024
@tuxtuxtuxtuxtux:opensuse.org@tuxtuxtuxtuxtux:opensuse.org left the room.10:13:47
3 Jan 2024
@malte:maltee.deMalte ECan I interpolate a heatmap while plotting? I have a function that takes two values as input and produces a third one as an output. I want to display the output on the x axis and the two inputs on the y and z axis. Typically, heatmaps are laid out on a grid, but I can't produce a grid because I cannot reverse my functions.09:11:29
13 Jan 2024
@psydroid:matrix.orgpsydroid changed their profile picture.21:03:16
15 Jan 2024
@psydroid:matrix.orgpsydroid changed their profile picture.08:33:11
@uncomfy:matrix.orguncomfy joined the room.12:50:50
22 Jan 2024
@iqbal1975:matrix.orgiqbal1975 set a profile picture.10:31:38
23 Jan 2024
@waldyrious:matrix.org@waldyrious:matrix.org left the room.22:14:03
28 Jan 2024
@.slt:matrix.org.slt joined the room.16:31:46
9 Feb 2024
@sirfredrick:matrix.org@sirfredrick:matrix.org left the room.00:03:25
@vwgsymnqkw:matrix.orgvwgsymnqkw joined the room.10:30:19
11 Feb 2024
@viplavswamituladhar:matrix.orgviplavswamituladhar joined the room.11:22:24
27 Feb 2024
@teru-ida:matrix.orgDai joined the room.23:50:05

There are no newer messages yet.


Back to Room ListRoom Version: