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]> 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]> http://www.perimeterinstitute.ca/personal/eschnetter/
