Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #7006

Re: Proposal for easier syntax for define-unless-already-defined pattern

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.ruby
Subject Re: Proposal for easier syntax for define-unless-already-defined pattern
Date 2014-07-30 19:41 +0200
Message-ID <c3sp27F40ebU1@mid.individual.net> (permalink)
References <fedb7003-f5e4-457b-9120-1ad937a9eedc@googlegroups.com> <bf498877-a244-4b0d-bab1-c4820534e7d7@googlegroups.com>

Show all headers | View raw


On 29.07.2014 18:44, Eugene Gilburg wrote:
> Forgot to add, the `?=` syntax should be context-sensitive to
> primitive types. E.g.
>
> some_hash[:some_member] ?= 'some_value'
>
> Should check for `some_hash.key?(:some_value)`

That is unlikely to happen.  The compiler cannot reliably determine that 
some_hash is a Hash for all cases and hence method #key? must be used 
instead of #defined?.

> Which will be assigned if the key does not exist, but not be assigned
> if the key exists but has `false` or `nil` value (another difference
> with the `||=` pattern).

The only way to make that happen is to require the object to also 
implement #key? besides of #[] and #[]= - but then for example Array 
would need this as well.  Which prompts the question what does #key? 
return for in index less than the Array's length where the value stored 
here is nil?

Kind regards

	robert


Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Proposal for easier syntax for define-unless-already-defined pattern Eugene Gilburg <eugene.gilburg@gmail.com> - 2014-07-29 09:40 -0700
  Re: Proposal for easier syntax for define-unless-already-defined pattern Eugene Gilburg <eugene.gilburg@gmail.com> - 2014-07-29 09:44 -0700
    Re: Proposal for easier syntax for define-unless-already-defined pattern Robert Klemme <shortcutter@googlemail.com> - 2014-07-30 19:41 +0200
  Re: Proposal for easier syntax for define-unless-already-defined pattern Robert Klemme <shortcutter@googlemail.com> - 2014-07-30 19:45 +0200

csiph-web