So how does a keyword init value work? 
Author Message
 So how does a keyword init value work?

The code (gd 2.3.2):

    define class <tagged> (<object>)
      slot tag :: <string>, init-keyword: tag-value:;
    end class <tagged>;

    define method print ( fd :: <stream>, self :: <tagged> )
      format( fd, "<%s>\n", self.tag );
    end method print;

    define class <version> (<tagged>)
      keyword tag-value: init-value: "squid";
    end class <version>;

    define method main(appname, #rest arguments)
      let y = make( <version> );
      print( *standard-output*, y );
      exit(exit-code: 0);
    end method main;

The problem:

Compiles ok, when run I get the following error:
    Accessing uninitialized slot tag in {an instance of <version>}

The DRM seems to indicate that this should work.  Shouldn't slot 'tag'
take the init-value: given in the class definition?  What should I do to
get this to work?  The idea being that each descendant of <tagged> will
get its own init-value: that will get printed.

Glenn



Fri, 08 Mar 2002 03:00:00 GMT  
 So how does a keyword init value work?
Hi Glenn,
   The tag slot defined in the <tagged> class is inherited
by your <version> class, so you need to state that in your
<version> class definition.

So,

define class <tagged> (<object>)
  slot tag :: <string>, init-keyword: tag-value:;
end class;

define class <version> (<tagged>)
  inherited slot tag, init-value: "squid";
end class;

This should allow every instance of <version> to have "squid"
by default and you can specify the value with your keyword
opassed to make

make(<version>, tag-value: "oyster");

btw, the way this is set up you can have different default
values for the tag slot for each class which inherits from
<tagged>, but you have to specify this value in each class
definition. If you want "squid" to be the default for every
class which inherits from <tagged>, you might want to move
the init-value declaration up into your <tagged> class
definition.

Hope this helps.

James

Quote:

> The code (gd 2.3.2):

>     define class <tagged> (<object>)
>       slot tag :: <string>, init-keyword: tag-value:;
>     end class <tagged>;

>     define method print ( fd :: <stream>, self :: <tagged> )
>       format( fd, "<%s>\n", self.tag );
>     end method print;

>     define class <version> (<tagged>)
>       keyword tag-value: init-value: "squid";
>     end class <version>;

>     define method main(appname, #rest arguments)
>       let y = make( <version> );
>       print( *standard-output*, y );
>       exit(exit-code: 0);
>     end method main;

> The problem:

> Compiles ok, when run I get the following error:
>     Accessing uninitialized slot tag in {an instance of <version>}

> The DRM seems to indicate that this should work.  Shouldn't slot 'tag'
> take the init-value: given in the class definition?  What should I do to
> get this to work?  The idea being that each descendant of <tagged> will
> get its own init-value: that will get printed.

> Glenn



Sat, 09 Mar 2002 03:00:00 GMT  
 So how does a keyword init value work?

Quote:

> The code (gd 2.3.2):

>     define class <version> (<tagged>)
>       keyword tag-value: init-value: "squid";
>     end class <version>;

Keyword clauses are broken. I've been working on it, but got
stalled. Please move along, there's nothing to see here ;).

Andreas

--
"Niemand hat die Absicht, eine Firewall einzurichten"



Sat, 09 Mar 2002 03:00:00 GMT  
 So how does a keyword init value work?

Quote:

>Hi Glenn,
>   The tag slot defined in the <tagged> class is inherited
>by your <version> class, so you need to state that in your
><version> class definition.

>So,

>define class <tagged> (<object>)
>  slot tag :: <string>, init-keyword: tag-value:;
>end class;

>define class <version> (<tagged>)
>  inherited slot tag, init-value: "squid";
>end class;

Yes, that works, but I still have a problem.  My code is adapted from the
DRM, Types and Classes-Instance Creation and Initialization-Initialization
Argument Specifications.

Let's take the example given:

    define class <person> (<object>)
      slot favorite-beverage, init-value: #"milk",
                      init-keyword: favorite-beverage:;
      slot name, required-init-keyword: name:; // my comma inserted
    end class <person>;

    define class <astronaut> (<person>)
      keyword favorite-beverage: init-value: #"tang";
      keyword name: init-value: "Bud";
    end class <astronaut>;

        // ...and to print it - my addition
        let x = make( <astronaut> );
        format-out( "%s likes to drink %s\n", x.name, x.favorite-beverage );

I get (gd 2.3.2) the following:
    Missing required-init-keyword #"name" in make of {the class <astronaut>}
but the DRM says:
    "In addition to indirectly supplying default values for these slots,
        this also has the effect of making the name: argument optional in calls
        to make on <astronaut>."
So I think that this should work, creating an astronaut named Bud.

Now try this:

    define class <person> (<object>)
      slot favorite-beverage, init-value: #"milk",
                      init-keyword: favorite-beverage:;
      slot name, init-value: #"John Doe"; // changed
    end class <person>;

    define class <astronaut> (<person>)
      keyword favorite-beverage: init-value: #"tang";
      inherited slot name, init-value: "Bud"; // changed
    end class <astronaut>;

I now get:
    Bud likes to drink milk
Right name, but shouldn't he like to drink "tang"?
        "the <astronaut> class provides default values for the favorite-
        beverage: [...] init-keywords."

And now remove the <person>.favorite-beverage init-value: on the
understanding that all descendant types will supply it:

    define class <person> (<object>)
      slot favorite-beverage, init-keyword: favorite-beverage:;
      slot name, init-value: #"John Doe";
    end class <person>;

And you get:
    Accessing uninitialized slot favorite-beverage in {an instance of
        <astronaut>}
which is where we started.  I have no idea if this is supposed to work,
to be honest, but I think it should.

So the question is : Is this code in the DRM meant to work?  Is my
understanding of what's meant to happen correct?  Have I found (yet :))
another bug/unwritten feature in Gwydion Dylan?  Am I a complete idiot?

Glenn the Curious
    (with apologies for the length)



Sat, 09 Mar 2002 03:00:00 GMT  
 So how does a keyword init value work?
Quote:


>> The code (gd 2.3.2):

>>     define class <version> (<tagged>)
>>       keyword tag-value: init-value: "squid";
>>     end class <version>;

>Keyword clauses are broken. I've been working on it, but got
>stalled. Please move along, there's nothing to see here ;).

Typical :).  I post a (rather long) essay on it and I discover that someone's
already posted the answer.  Thanks anyway.

Glenn



Sun, 10 Mar 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Dylan Tip #101: don't use keyword init-values with class allocated slots

2. __init__ keyword param for sub-class?

3. new keyword idea: tryreturn, tryturn or done

4. Keyword-Value-Arguments: Why?

5. value of a keyword

6. init nets values + undocumented features in VerilogXL

7. Keyword-Value-Arguments: Why?

8. Changing Vantage VHDL Simulator Init. Value

9. How can i get values from a init - file

10. Question on default value of __init__

11. no return values for __init__ ??

12. Best style for **keyword arg default values?

 

 
Powered by phpBB® Forum Software