Use a Dict.

  -- John

On Oct 6, 2014, at 10:37 AM, Tamas Papp <[email protected]> wrote:

> Possibly -- I don't know Julia that well. Still, my understanding is
> that code like
> 
> --8<---------------cut here---------------start------------->8---
> type MyResult
>  a
>  b
> end
> 
> function foo()
>  MyResult(1,2)
> end
> 
> function bar(result)
>  result.a
> end
> --8<---------------cut here---------------end--------------->8---
> 
> has the following disadvantages:
> 
> 1. I need to define the type, which is a bit gratuitous for my purposes
> (for quick prototyping),
> 2. I have to remember the order of arguments for the constructor (of
> course, I could work around that, with more boilerplate and then
> eventually a macro),
> 3. redefinition won't be allowed in Main.
> 
> 1-2 are minor issues, 3 is cumbersome.
> 
> So I am still interested in what the lightweight alternative is. So let
> me clarify the question: if I don't want to use composite types, what is
> the idiomatic way to do a lightweight named list? Tuples of tuples, or a
> Dict?
> 
> Best,
> 
> Tamas
> 
> On Mon, Oct 06 2014, John Myles White <[email protected]> wrote:
> 
>> I think you're underselling (1), which provides the compiler with exactly 
>> the kind of information it needs to generate extremely efficient code while 
>> also providing names.
>> 
>> -- John
>> 
>> On Oct 6, 2014, at 8:44 AM, Tamas Papp <[email protected]> wrote:
>> 
>>> Hi,
>>> 
>>> AFAIK tuples are the recommended way to return multiple values from a
>>> function, but this requires that the programmer remembers the order of
>>> values. I am wondering what the correct idiom is for returning key/value
>>> pairs, similarly to alist/plist in Common Lisp. I have considered the
>>> following alternatives:
>>> 
>>> 1. defining a composite type.
>>> 
>>> advantages: well-defined constructor and extractor methods.
>>> 
>>> disavantages: overkill for most situations, especially if used in 1-2
>>> places. redefinition cumbersome.
>>> 
>>> 2. using hash tables (Dict), especially with their literal [ key1 =>
>>> value1 ] syntax.
>>> 
>>> advantages: lightweight, can use symbols as keys. well-defined error
>>> when key not found.
>>> 
>>> disadvantages: did not benchmark it, but not sure they are
>>> lightweight, for 2-5 values; hash tables usually aren't.
>>> 
>>> 3. tuples of tuples. This is apparently what keywords arguments use:
>>> 
>>> http://julia.readthedocs.org/en/latest/manual/functions/#keyword-arguments
>>> 
>>> "Inside f, args will be a collection of (key,value) tuples, where each
>>> key is a symbol. Such collections can be passed as keyword arguments
>>> using a semicolon in a call, e.g. f(x, z=1; args...). Dictionaries can
>>> be used for this purpose."
>>> 
>>> advantages: seems to be the internal mechanism.
>>> 
>>> disadvantages: could not (yet) find the julia equivalent of cl:assoc
>>> etc.
>>> 
>>> Please advise me, I would like to use the proper style.
>>> 
>>> Best,
>>> 
>>> Tamas

Reply via email to