From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id 6E4B219C0022 for ; Fri, 30 Oct 2015 05:32:44 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 53326B5D8CA for ; Fri, 30 Oct 2015 06:00:51 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id 464E9952439 for ; Fri, 30 Oct 2015 06:00:50 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 845321204CA; Fri, 30 Oct 2015 06:00:48 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from mail-qg0-f53.google.com (mail-qg0-f53.google.com [209.85.192.53]) by neon.ruby-lang.org (Postfix) with ESMTPS id 9C0A812047A for ; Fri, 30 Oct 2015 06:00:43 +0900 (JST) Received: by qgad10 with SMTP id d10so45690300qga.3 for ; Thu, 29 Oct 2015 14:00:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:content-type; bh=R8eWyvMiFJHTkfmX6kMGCZdcGL75Y8Op8rn2eMSqp78=; b=uD+H8l4hCaw+OetkETSeLdixOV1iIx5ahcCtIjl3IafeLTDHEefnxhN6EwJfmPjgaa ZjxOi9diJCIMlcxBDQY6UP4AMSFJHCnPVW/yUjvfi3gHccuFqokfrrhktWnINF4WEtUN dbj0qO2SCIydWKNPrfmAAl1rwLKfBGTv9QSokkar1AtxbV1syZr1CtY+naWUMVMjQ7Ui Y6AyDvxsJamZCg9uVFRrd7qtS1taBki7+3Q2aEo/sSLBX+lggPrqfQkPErDxRusi9Fez pojzHwENeUN8WP6us9CM2TlbSN/5X9cY3XvSY14bb+btKCoSPbVAsJPdnR9/nnpif2TW RGbw== MIME-Version: 1.0 X-Received: by 10.140.148.74 with SMTP id 71mr5658926qhu.26.1446152441543; Thu, 29 Oct 2015 14:00:41 -0700 (PDT) Received: by 10.55.104.2 with HTTP; Thu, 29 Oct 2015 14:00:41 -0700 (PDT) Received: by 10.55.104.2 with HTTP; Thu, 29 Oct 2015 14:00:41 -0700 (PDT) In-Reply-To: References: Date: Fri, 30 Oct 2015 07:00:41 +1000 X-Google-Sender-Auth: -7i8CB29qCOI7sEWZsDSAzNxCE0 Message-ID: From: Matthew Kerwin To: Ruby developers Content-Type: multipart/alternative; boundary=001a11355ea091c07d0523449a80 X-ML-Name: ruby-core X-Mail-Count: 71273 Subject: [ruby-core:71273] Re: [Ruby trunk - Feature #11537] Introduce "Safe navigation operator" X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" --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, 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 = a.b.c.d; //this would fail with error if a, b or c are null or > undefined. > var current = a && a.b && a.b.c && a.b.c.d; // the current messy way to > handle this > var currentBrackets = a && a['b'] && a['b']['c'] && a['b']['c']['d']; > //the current messy way to handle this > var typeScript = a?.b?.c?.d; // The typescript way of handling the above > mess with no errors > var typeScriptBrackets = a?['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 = a..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 = a..['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 = x[1].y; //works > var missing= x[2].y; //throws exception > var assume= x && 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 = x..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 = x..1..y; // should work as well, resulting 'test' in this case > var err = x..2..y; // should work as well, resulting undefined in this case > ``` > > > With dynamic names: > ```js > var correct1 = x..[1]..y; //would work returning 'test' > var correct2 = x..[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 = condition ? a?.x.?y : a?.y?.z; > var c = condition ? a?['x']?['y'] : a?['y']?['z']; > ``` > as opposed to double dots `..` and `..['prop']` > ```js > var a = { x: { y: 1 } }; > var b = condition ? a..x..y : a..y..z; > var c = condition ? 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 = User.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 = User.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

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 Laurenti= u 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 u= sing the ternary ? : operator on the same line.

If it's not a conflict in Ruby syntax perhaps worths looking at

https://esdiscu= ss.org/topic/existential-operator-null-propagation-operator#content-65<= br> 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 =3D a.b.c.d; //this would fail with error if a, b or c are null o= r undefined.
var current =3D a && a.b && a.b.c && a.b.c.d; // th= e current messy way to handle this
var currentBrackets =3D a && a['b'] && a['b'= ;]['c'] && a['b']['c']['d']; //the = current messy way to handle this
var typeScript =3D a?.b?.c?.d; // The typescript way of handling the above = mess with no errors
var typeScriptBrackets =3D a?['b']?['c']?['d']; //T= he 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 =3D a..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 =3D a..['b']..['c']..['d'= ;];
```

For the bracket notation, I recommend two dots instead of a single one as i= t's consistent with the others when non brackets are used. Hence only t= he 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 unde= fined).

Two dots make it more clear, more visible and more space-wise so you unders= tand 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 =3D {};
x.1 =3D {y: 'test' }; //fails currently
x[1] =3D {y: 'test' }; //works currently
var current =3D x[1].y; //works
var missing=3D x[2].y; //throws exception
var assume=3D x && x[2] && x[2].y; // works but very messy<= br> ```

About numbers two options: Your call which one can be adopted, but I recomm= end first one for compatibility with existing rules!
1. Should fail as it does now (`x.1.y` =3D=3D `runtime error`)
```js
var err =3D x..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 =3D x..1..y; // should work as well, resulting 'test' in th= is case
var err =3D x..2..y; // should work as well, resulting undefined in this ca= se
```


With dynamic names:
```js
var correct1 =3D x..[1]..y; //would work returning 'test'
var correct2 =3D x..[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 conf= using on the same line.

e.g. using `?.` and `?['prop']`
```js
var a =3D { x: { y: 1 } };
var b =3D condition ? a?.x.?y : a?.y?.z;
var c =3D condition ? a?['x']?['y'] : a?['y']?['= ;z'];
```
as opposed to double dots `..` and `..['prop']`
```js
var a =3D { x: { y: 1 } };
var b =3D condition ? a..x..y : a..y..z;
var c =3D condition ? 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-5= 4644

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u =3D User.find(id)
if u && u.profile && u.profile.thumbnails && u.prof= iles.thumbnails.large
=C2=A0 ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
=C2=A0...
```
I hope to write shortly above code. Groovy has above operator named "S= afe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u =3D User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




--
https://bugs.ruby-lang.org/
--001a11355ea091c07d0523449a80--