I have found a solution to both problems I raised, thanks to some helpful 
feedback, although I am not completely happy with the second solution.

For the problem of defining a no-argument constructor for my Map class, I 
made a mistake in the declaration:
   type Map{K,D}
       ...
       function Map{K,D}()
       ....
       end
   end

The mistake is that the inner constructor should be declared "function 
Map()" because the {K,D} parameters are already presumed.

For the second problem of how to construct records that have K and D fields 
that are dummy placeholders, I did not find a solution.  So instead I 
changed my implementation so that the map is initially created in a null 
state in which even the placeholders are absent.  The first call to 
"insert!" will have proper K and D values, so I use those K and D arguments 
also to insert the two dummy placeholders to put the data structure into 
its normal state (plus the given (K,D) pair is also inserted).

I'm not completely happy with this solution for two reasons.  First, all 
methods associated with Map{K,D} are now more complicated because they 
first need to make sure that it is not in the null state.  Second, if the 
first insertion involves a particular large {K,D} pair, the user of the 
data structure might be surprised that his/her initial {K,D} pair continues 
to occupy memory even when it is deleted from the data structure (because 
it is still being held down by the two placeholders).  So I welcome further 
suggestions for addressing the issue of finding dummy K and D values to use 
in placeholder records.

Thanks,
Steve Vavasis
 

On Wednesday, June 18, 2014 2:41:01 PM UTC+3, [email protected] wrote:
>
> Dear Julia colleagues,
>
> Earlier I asked whether there is a datatype similar to "map<K,D>" from 
> C++, and nobody answered, so I decided as my first nontrivial Julia program 
> to write such a type.  This is an associative array (i.e., a dictionary), 
> with the added property that the keys have a sort-order, and it is possible 
> to loop over the items in the array in this order.  I am using a 2-3 tree 
> as the implementation.  My main data structure looks like this:
>
> type Map{K,D}
>    ...
>     function Map{K,D}()
>     ....
>     end
> end
>
> But I am stuck right at the outset with two very basic Julia questions. 
>  First, what is the syntax for a user of my datatype to declare a variable 
> to be an empty map of a certain type?  In other words, suppose the user 
> wants a map from strings to strings:
>
> function test1()
>     m1 = Map{ASCIIString, ASCIIString}()
>     m1["hello"] = "jello"
> end
>
>
> This gave the error: ERROR: no method Map{ASCIIString, ASCIIString}().   
> What is the right syntax to invoke the default constructor for an empty 
> map?  I couldn't figure this out from the manual; in the manual, the 
> constructors either take arguments or else they are constructors for 
> built-ins (like arrays) that have a special syntax like a = Int[].
>  
> Second, at various points in the code, I need to use a default or blank 
> instance of either K (the key type) or D (the datatype).  In other words, I 
> need to initialize an array element whose type is a composite type that has 
> some K or D fields; these need to be initialized to some arbitrary K or D 
> value that won't ever be used. What is the proper syntax to initialize a 
> field k to have a value of type K (where K is a parameter type in my code), 
> where the value doesn't matter (can be any default).
>
> Thanks,
> Steve Vavasis
>
>

Reply via email to