Dne čtvrtek 26. května 2016 17:40:34 UTC+2 Yichao Yu napsal(a): > > On Thu, May 26, 2016 at 11:31 AM, Ford Ox <[email protected] <javascript:>> > wrote: > > Lets start from scratch. > > > > What if this syntax would be possible in Julia: > > > > collection = [i for i in 1:10] > > @show collection[:first] > > @show collection[:middle] > > @show collection[:last] > > @show collection[:first] = -1 > > @show collection[:middle] = -1 > > @show collection[:my_own_name] = -1 > > I don't see why it is better than using a keyword like what we have > now. Especially because `middle` is confusing and symbol is valid > syntax already so it can't be used for compile time computation. >
Because it is done at compile time, where you can even decide to call completely different function. See dequeue = ..... dequeue[:first] # is replaced with different function than dequeue[1] dequeue[:last] # is replaced with different function than dequeue[end] ( O(1) vs O(n) ) Because you can define your own syntax. The :middle was just stupid example to show you, that you can decide on your own symbol based on collection you are working with ( say there is collection which when accessing middle has different complexity than accessing other elements). > > >> > >> collection[1] = 1 > >> collection[5] = 5 > >> collection[10] = 10 > >> collection[1] = -1 = -1 > >> collection[5] = -1 = -1 > >> collection[7] = -1 = -1 > > > > > > As you see, it is decided at compile time, at what index of array I > access > > And you can create special symbols for this smart accessing in julia > itself! > > I don't see why it's useful since we support doing arithmetics on > `end` (and I don't see how you can do that (arithmetics) with your > approach). > Of course you can do arithmetics with this approach. > > > > > Fact: It is already possible, but it does not look so nice: > > @setindex ( collection[:first] = 5 ) > > Fact: You cannot do something like: > > FIRST = :first > > @setindex ( collection[FIRST] = 5 ) > > Which is what make the way you propose unsatisfying. > > Are you talking about [ FIRST = :first ]? If so, I could argue about this since you can't also do [ LAST = end ] The only difference is that [ LAST = end ] throws error at compile time.
