Sender | Message | Time |
---|---|---|
28 Dec 2023 | ||
dionisos | *
| 18:04:38 |
dionisos | In 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 | * 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:
| 18:39:38 |
qwjyh | If the type need only one function, then you can use
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 | * If the type need only one function, then you can use
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 | 19:51:01 |
qwjyh | * 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 | In reply to @qwjyh:matrix.orgThank you for your answer | 23:29:20 |
dionisos | I admit I doesn’t know the syntax you are using, I will try to understand how it works | 23:30:01 |
dionisos | I mean the "(v::MyType)" | 23:30:14 |
dionisos |
| 23:57:18 |
dionisos | When I try like that :
| 23:58:20 |
29 Dec 2023 | ||
dionisos | Ok I found how to do that, it should be :
But I admit I don’t really understand why. | 00:04:23 |
dionisos | Also I am curious about the "(test::MyType)" syntax | 00:05:41 |
qwjyh | 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. | 00:06:12 |
dionisos | * Ok I found how to do that, it should be :
But I admit I don’t really understand why. | 00:07:15 |
dionisos | In reply to @qwjyh:matrix.orgYes this is what I want | 00:09:11 |
dionisos | Oh, I just realized the "where MyType", just make MyType a variable | 00:10:03 |
qwjyh | 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 ``` | 00:10:04 |
dionisos | In reply to @qwjyh:matrix.org My problem is that the function is in a closure, and it gives me the error :
| 00:11:31 |
dionisos | I am very probably misunderstanding something. | 00:12:22 |
qwjyh | ``` 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 | I 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 | In reply to @qwjyh:matrix.orgI realize I misunderstood you question, but at least now I understand the syntax you where using, thank you. | 00:25:22 |
dionisos | Ok after some testes and research, it seems like I should use a macro for this. | 00:27:20 |
dionisos | Thank you for you help qwjyh | 00:27:34 |
dionisos | In reply to @qwjyh:matrix.org* 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 set a profile picture. | 14:54:13 | |
@tuxtuxtuxtuxtux:opensuse.org removed their profile picture. | 14:54:31 | |
@tuxtuxtuxtuxtux:opensuse.org set a profile picture. | 14:56:09 | |
@tuxtuxtuxtuxtux:opensuse.org removed their profile picture. | 14:57:05 |