--001a11355ea091c07d0523449a80
Content-Type: text/plain; charset=UTF-8

a..b already means something in ruby

I'm happy with Matz's acceptance of .? especially in light of all the
discussions that lead up to it.
On 30/10/2015 5:33 AM, <alonecomp / gmail.com> wrote:

> Issue #11537 has been updated by Laurentiu Macovei.
>
>
> The same discussion happens to be on TypeScript and ES6 worlds.
> Using .. instead of ?. or .? because it's way more clear when you are
> using the ternary ? : operator on the same line.
>
> If it's not a conflict in Ruby syntax perhaps worths looking at
>
>
> https://esdiscuss.org/topic/existential-operator-null-propagation-operator#content-65
> https://github.com/Microsoft/TypeScript/issues/16#issuecomment-152275052
>
> Posting the markdown info from there:
>
> This would be amazing operator!! Especially for `ES6`/`ES7`/`TypeScript` -
> and why not `Ruby` ?
>
> ```js
> var error  .b.c.d; //this would fail with error if a, b or c are null or
> undefined.
> var current   && a.b && a.b.c && a.b.c.d; // the current messy way to
> handle this
> var currentBrackets   && a['b'] && a['b']['c'] && a['b']['c']['d'];
> //the current messy way to handle this
> var typeScript  ?.b?.c?.d; // The typescript way of handling the above
> mess with no errors
> var typeScriptBrackets  ?['b']?['c']?['d']; //The typescript of handling
> the above mess with no errors
> ```
> However I propose a more clear one - as not to confuse ? from the a ? b :
> c statements with a?.b statements:
>
> ```js
> var doubleDots  ..b..c..d; //this would be ideal to understand that you
> assume that if any of a, b, c is null or undefined the result will be null
> or undefined.
> var doubleDotsWithBrackets  ..['b']..['c']..['d'];
> ```
>
> For the bracket notation, I recommend two dots instead of a single one as
> it's consistent with the others when non brackets are used. Hence only the
> property name is static or dynamic via brackets.
>
> Two dots, means if its null or undefined stop processing further and
> assume the result of expression is null or undefined. (as d would be null
> or undefined).
>
> Two dots make it more clear, more visible and more space-wise so you
> understand what's going on.
>
> This is not messing with numbers too - as is not the same case e.g.
>
> ```js
> 1..toString(); // works returning '1'
> var x  };
> x.1  y: 'test' }; //fails currently
> x[1]  y: 'test' }; //works currently
> var current  [1].y; //works
> var missing [2].y; //throws exception
> var assume  && x[2] && x[2].y; // works but very messy
> ```
>
> About numbers two options: Your call which one can be adopted, but I
> recommend first one for compatibility with existing rules!
> 1. Should fail as it does now (`x.1.y` `runtime error`)
> ```js
> var err  ..1..y; // should fail as well, since 1 is not a good property
> name, nor a number to call a method, since it's after x object.
> ```
> 2. Should work since it understands that is not a number calling a
> property from `Number.prototype`
> ```js
> var err  ..1..y; // should work as well, resulting 'test' in this case
> var err  ..2..y; // should work as well, resulting undefined in this case
> ```
>
>
> With dynamic names:
> ```js
> var correct1  ..[1]..y; //would work returning 'test'
> var correct2  ..[2]..y; //would work returning undefined;
> ```
>
> What do you think folks?
>
> P.S. `foo?.bar` and `foo?['bar']` syntax would work too.
>
> However the using both current `?` `:` operator and `?.` might be very
> confusing on the same line.
>
> e.g. using `?.` and `?['prop']`
> ```js
> var a   x: { y: 1 } };
> var b  ondition ? a?.x.?y : a?.y?.z;
> var c  ondition ? a?['x']?['y'] : a?['y']?['z'];
> ```
> as opposed to double dots `..` and `..['prop']`
> ```js
> var a   x: { y: 1 } };
> var b  ondition ? a..x..y : a..y..z;
> var c  ondition ? a..['x']..['y'] : a..['y']..['z'];
> ```
>
> ##### Which one does look more clear to you?
>
> ----------------------------------------
> Feature #11537: Introduce "Safe navigation operator"
> https://bugs.ruby-lang.org/issues/11537#change-54644
>
> * Author: Hiroshi SHIBATA
> * Status: Closed
> * Priority: Normal
> * Assignee: Yukihiro Matsumoto
> ----------------------------------------
> I sometimes write following code with rails application:
>
> ```ruby
> u  ser.find(id)
> if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
>   ...
> ```
>
> or
>
> ```ruby
> # Use ActiveSupport
> if u.try!(:profile).try!(:thumbnails).try!(:large)
>  ...
> ```
> I hope to write shortly above code. Groovy has above operator named "Safe
> navigation operator" with "`?.`" syntax.
> Ruby can't use "`?.`" operator.
>
> Can we use "`.?`" syntax. like this:
>
> ```ruby
> u  ser.find(id)
> u.?profile.?thumbnails.?large
> ```
>
> Matz. How do you think about this?
>
>
>
>
> --
> https://bugs.ruby-lang.org/
>

--001a11355ea091c07d0523449a80
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<p dir="ltr">a..b already means something in ruby</p>
<p dir="ltr">I&#39;m happy with Matz&#39;s acceptance of .? especially inight of all the discussions that lead up to it.</p>
<div class="gmail_quote">On 30/10/2015 5:33 AM,  &lt;alonecomp / gmail.com&gt; wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Issue #11537 has been updated by Laurentiu Macovei.<br>
<br>
<br>
The same discussion happens to be on TypeScript and ES6 worlds.<br>
Using .. instead of ?. or .? because it&#39;s way more clear when you are using the ternary ? : operator on the same line.<br>
<br>
If it&#39;s not a conflict in Ruby syntax perhaps worths looking at<br>
<br>
<a href="https://esdiscuss.org/topic/existential-operator-null-propagation-operator#content-65" rel="noreferrer" target="_blank">https://esdiscuss.org/topic/existential-operator-null-propagation-operator#content-65</a><br>
<a href="https://github.com/Microsoft/TypeScript/issues/16#issuecomment-152275052" rel="noreferrer" target="_blank">https://github.com/Microsoft/TypeScript/issues/16#issuecomment-152275052</a><br>
<br>
Posting the markdown info from there:<br>
<br>
This would be amazing operator!! Especially for `ES6`/`ES7`/`TypeScript` - and why not `Ruby` ?<br>
<br>
```js<br>
var error = a.b.c.d; //this would fail with error if a, b or c are null or undefined.<br>
var current = a &amp;&amp; a.b &amp;&amp; a.b.c &amp;&amp; a.b.c.d; // the current messy way to handle this<br>
var currentBrackets = a &amp;&amp; a[&#39;b&#39;] &amp;&amp; a[&#39;b&#39;][&#39;c&#39;] &amp;&amp; a[&#39;b&#39;][&#39;c&#39;][&#39;d&#39;]; //the current messy way to handle this<br>
var typeScript = a?.b?.c?.d; // The typescript way of handling the above mess with no errors<br>
var typeScriptBrackets = a?[&#39;b&#39;]?[&#39;c&#39;]?[&#39;d&#39;]; //The typescript of handling the above mess with no errors<br>
```<br>
However I propose a more clear one - as not to confuse ? from the a ? b : ctatements with a?.b statements:<br>
<br>
```js<br>
var doubleDots = a..b..c..d; //this would be ideal to understand that youssume that if any of a, b, c is null or undefined the result will be nullr undefined.<br>
var doubleDotsWithBrackets = a..[&#39;b&#39;]..[&#39;c&#39;]..[&#39;d&#39;];<br>
```<br>
<br>
For the bracket notation, I recommend two dots instead of a single one as it&#39;s consistent with the others when non brackets are used. Hence only the property name is static or dynamic via brackets.<br>
<br>
Two dots, means if its null or undefined stop processing further and assumehe result of expression is null or undefined. (as d would be null or undefined).<br>
<br>
Two dots make it more clear, more visible and more space-wise so you understand what&#39;s going on.<br>
<br>
This is not messing with numbers too - as is not the same case e.g.<br>
<br>
```js<br>
1..toString(); // works returning &#39;1&#39;<br>
var x = {};<br>
x.1 = {y: &#39;test&#39; }; //fails currently<br>
x[1] = {y: &#39;test&#39; }; //works currently<br>
var current = x[1].y; //works<br>
var missing= x[2].y; //throws exception<br>
var assume= x &amp;&amp; x[2] &amp;&amp; x[2].y; // works but very messy<br>
```<br>
<br>
About numbers two options: Your call which one can be adopted, but I recommend first one for compatibility with existing rules!<br>
1. Should fail as it does now (`x.1.y` == `runtime error`)<br>
```js<br>
var err = x..1..y; // should fail as well, since 1 is not a good propertyame, nor a number to call a method, since it&#39;s after x object.<br>
```<br>
2. Should work since it understands that is not a number calling a propertyrom `Number.prototype`<br>
```js<br>
var err = x..1..y; // should work as well, resulting &#39;test&#39; in this case<br>
var err = x..2..y; // should work as well, resulting undefined in this case<br>
```<br>
<br>
<br>
With dynamic names:<br>
```js<br>
var correct1 = x..[1]..y; //would work returning &#39;test&#39;<br>
var correct2 = x..[2]..y; //would work returning undefined;<br>
```<br>
<br>
What do you think folks?<br>
<br>
P.S. `foo?.bar` and `foo?[&#39;bar&#39;]` syntax would work too.<br>
<br>
However the using both current `?` `:` operator and `?.` might be very confusing on the same line.<br>
<br>
e.g. using `?.` and `?[&#39;prop&#39;]`<br>
```js<br>
var a = { x: { y: 1 } };<br>
var b = condition ? a?.x.?y : a?.y?.z;<br>
var c = condition ? a?[&#39;x&#39;]?[&#39;y&#39;] : a?[&#39;y&#39;]?[&#39;z&#39;];<br>
```<br>
as opposed to double dots `..` and `..[&#39;prop&#39;]`<br>
```js<br>
var a = { x: { y: 1 } };<br>
var b = condition ? a..x..y : a..y..z;<br>
var c = condition ? a..[&#39;x&#39;]..[&#39;y&#39;] : a..[&#39;y&#39;]..[&#39;z&#39;];<br>
```<br>
<br>
##### Which one does look more clear to you?<br>
<br>
----------------------------------------<br>
Feature #11537: Introduce &quot;Safe navigation operator&quot;<br>
<a href="https://bugs.ruby-lang.org/issues/11537#change-54644" rel="noreferrer" target="_blank">https://bugs.ruby-lang.org/issues/11537#change-54644</a><br>
<br>
* Author: Hiroshi SHIBATA<br>
* Status: Closed<br>
* Priority: Normal<br>
* Assignee: Yukihiro Matsumoto<br>
----------------------------------------<br>
I sometimes write following code with rails application:<br>
<br>
```ruby<br>
u = User.find(id)<br>
if u &amp;&amp; u.profile &amp;&amp; u.profile.thumbnails &amp;&amp; u.profiles.thumbnails.large<br>
 ...<br>
```<br>
<br>
or<br>
<br>
```ruby<br>
# Use ActiveSupport<br>
if u.try!(:profile).try!(:thumbnails).try!(:large)<br>
...<br>
```<br>
I hope to write shortly above code. Groovy has above operator named &quot;Safe navigation operator&quot; with &quot;`?.`&quot; syntax.<br>
Ruby can&#39;t use &quot;`?.`&quot; operator.<br>
<br>
Can we use &quot;`.?`&quot; syntax. like this:<br>
<br>
```ruby<br>
u = User.find(id)<br>
u.?profile.?thumbnails.?large<br>
```<br>
<br>
Matz. How do you think about this?<br>
<br>
<br>
<br>
<br>
--<br>
<a href="https://bugs.ruby-lang.org/" rel="noreferrer" target="_blank">https://bugs.ruby-lang.org/</a><br>
</blockquote></div>

--001a11355ea091c07d0523449a80--