Attempted roadmap of future instance variables....

Discussion in 'Ruby' started by David A. Black, Dec 2, 2003.

  1. Hi --

    I figure that I owe the world at least a shot at clearing the cobwebs
    (of my own creation, that is :) on this instance variable thing.

    So... for those still reading... here's what I *really* think is being
    proposed:


    class A
    def m
    @var = "hi" # regular instance variable
    @_var = "hello" # class-local instance variable
    end
    end

    class B < A
    def n
    puts @var # same as @var, above
    puts @_var # not same as @_var above
    end
    end

    o = B.new
    o.m
    o.n

    => Output:

    hi # the @var that was set in A#m is used here
    nil # the @_var that was in A#m is *not* used here
    # (because we're now in a method defined in B,
    # a subclass of A, and class local instance
    # variables are not shared by subclasses)

    In other words:

    * Regular instance variables live per-name per-object
    (o has exactly one @var).

    * Class-local instance variables live per-name per-object per-class
    (o's methods defined in A have a @_var, and o's methods defined in
    B have a different @_var).


    David

    --
    David A. Black
     
    David A. Black, Dec 2, 2003
    #1
    1. Advertising

  2. David A. Black

    Ben Giddings Guest

    David A. Black wrote:
    > So... for those still reading... here's what I *really* think is being
    > proposed:
    >
    > class A
    > def m
    > @var = "hi" # regular instance variable
    > @_var = "hello" # class-local instance variable
    > end
    > end


    For what it's worth, I don't think this is a good syntax. To me, an
    underscore is an alphanumeric character. It's special because it is the
    only non-letter, non-number that can appear in the middle of a variable name:

    @foo_bar # legal
    @foo.bar # not the same thing
    @foo@bar # illegal
    ...

    Making @_ a special case is bound to be confusing, because of this. There
    is also the fact that normally variable names with a leading underscore are
    a notation that something is internal, and not normally enforced as
    something different.

    On the other hand, this is consistent with other instances where the first
    character of a symbol modifies its behaviour (i.e. if it starts with an
    uppercase letter, it is a constant).

    Mainly, I suppose, this just brings back ugly memories of languages that
    lack truly private variables, so resort to notational hacks like leading
    underscores.

    Maybe a leading underscore by itself would be less confusing/distasteful?

    class A
    def m
    @var = "hi" # regular instance variable
    _var = "hello" # class-local instance variable
    end
    end

    Ben
     
    Ben Giddings, Dec 3, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?aG9sYW4=?=

    Roadmap for ATLAS?

    =?Utf-8?B?aG9sYW4=?=, Mar 1, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    429
    Alvin Bruney - ASP.NET MVP
    Mar 1, 2006
  2. Dave Stallard

    Roadmap for Java 1.5?

    Dave Stallard, Sep 17, 2004, in forum: Java
    Replies:
    2
    Views:
    404
    Dave Stallard
    Sep 17, 2004
  3. Frank D. Greco
    Replies:
    0
    Views:
    354
    Frank D. Greco
    Mar 22, 2005
  4. K_Lee
    Replies:
    0
    Views:
    469
    K_Lee
    Nov 11, 2003
  5. T. Onoma
    Replies:
    5
    Views:
    147
    T. Onoma
    Dec 6, 2003
Loading...

Share This Page