Thanks for pointing out the mistakes.
I want to loop over the data types because I want to loop over functions.
I need to do something like this:
function mywork(x, y, z)
procedure1(x, y, z)
procedure2(x, y, z)
procedure3(x, y, z)
#...
procedure8(x, y, z)
procedure9(x, y, z)
procedure10(x, y, z)
end
I feel it may be silly to write out all the procedurek(x, y, z)
Then I am thinking about the following:
function mywork(x, y, z)
for k in 1:10
procedure(::Val{k}, x, y, z)
end
end
I have not seen anyone doing this before. So I am not sure I am doing
something right.
That's why I am asking this question.
I remember people like to use macro to define functions for different types.
In my case, should I use macro? Or am I fine to code as above?
On Friday, June 10, 2016 at 7:34:09 PM UTC-7, Erik Schnetter wrote:
>
> Your code won't work. `Val{k}` is a type, which is an object in Julia. You
> are passing this object to `foo`. Thus in your declaration of `foo`, you
> need to list the type of `Val`, not just `Val` -- types have types as well:
> ```
> foo(::Type{Val{1}}) = 1
> ```
>
> Of course you know that using `Val` in this way is nonsensical in a real
> program. I understand that you know this, as you're purposefully
> experimenting with Julia, but I'd still like to point it out for the casual
> reader of this example.
>
> Whether you encounter "performance issues" or not depends on what
> performance you need. If you compare this code to simple arithmetic
> operations (adding numbers), then it's slower. If you compare it to sending
> data across the network or accessing the disk, then it's faster.
>
> I assume that calling `foo` in the loop requires a hash table lookup at
> run time, and likely some memory allocation.
>
> -erik
>
>
> On Fri, Jun 10, 2016 at 9:40 PM, Po Choi <[email protected]
> <javascript:>> wrote:
>
>> Ops! I accidentally hit the post button! So the post is not completed!
>>
>> It is an example:
>> foo(::Val{1}) = 1
>> foo(::Val{2}) = 2
>> foo(::Val{2}) = 3
>>
>> function bar()
>> s = 0
>> for t in Datatype[Val{k} for k in 1:3]
>> s += foo(t)
>> end
>> end
>>
>> Will there be any performance issue if I loop over types?
>> I am still trying to understand how the multiple-dispatch works.
>> Sometimes I am confused!
>>
>>
>> On Friday, June 10, 2016 at 6:37:31 PM UTC-7, Po Choi wrote:
>>>
>>>
>>>
>>> foo(::Val{1}) = 1
>>> foo(::Val{2}) = 2
>>> foo(::Val{2}) = 3
>>>
>>> function bar()
>>>
>>> for t in Datatype[Val{k} for k in 1:3]
>>>
>>> end
>>> end
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>
>
> --
> Erik Schnetter <[email protected] <javascript:>>
> http://www.perimeterinstitute.ca/personal/eschnetter/
>