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 C81E419C0020 for ; Fri, 23 Oct 2015 01:02:48 +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 39014B5D875 for ; Fri, 23 Oct 2015 01:30:00 +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 3A3A8952439 for ; Fri, 23 Oct 2015 01:29:57 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id C3C721204D0; Fri, 23 Oct 2015 01:29:56 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o2.heroku.sendgrid.net (o2.heroku.sendgrid.net [67.228.50.55]) by neon.ruby-lang.org (Postfix) with ESMTPS id ADF791204B3 for ; Fri, 23 Oct 2015 01:29:52 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=qz3TsoAl6V/azLC/iiQVcslqhLQ=; b=vELXbDIbkWHpFO60ye BgT6NCztB5x+qMq2hs6mWhWBO+gtOdNtl8RA3e/d3kiSdLwuzlFGcbY0ANFqBA10 +Z/URoRn7lYhmJAklFKpiAIVXeH6h0l2E+Wi6/5rg5GuFXr+7ZsPH5Hk9h1K68LZ 7XCXdIl4lrhjEBsIeS+SAxw4M= Received: by filter0542p1mdw1.sendgrid.net with SMTP id filter0542p1mdw1.8840.56290EF975 2015-10-22 16:29:45.862400971 +0000 UTC Received: from herokuapp.com (ec2-54-196-88-67.compute-1.amazonaws.com [54.196.88.67]) by ismtpd0005p1iad1.sendgrid.net (SG) with ESMTP id v5tejxPCRSO-O0llynqVww for ; Thu, 22 Oct 2015 16:29:45.896 +0000 (UTC) Date: Thu, 22 Oct 2015 16:29:45 +0000 From: treznick@continuity.net To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Redmine-MailingListIntegration-Message-Ids: 45783 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11537 X-Redmine-Issue-Author: hsbt X-Redmine-Issue-Assignee: matz X-Redmine-Sender: treznick X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS79ysyq/+pShKLRs56FQi3D0TAzK546lomROr 7tiy3Ae010mT1YYbC6gCQCkDlkgIRxxteBn6h7LTkXtVQzg3K14YWrnq3g9WPd46myb9mCN3aH+qiG 4kZy09E4olPqEjQIQah/s4UgRTQtklihUo88 X-ML-Name: ruby-core X-Mail-Count: 71157 Subject: [ruby-core:71157] [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" Issue #11537 has been updated by Tom Reznick. Hi, I think we may have found some unexpected behavior with the `.?` operator. If I call the following: ``` s = Struct.new(:x) o = s.new() o.x #=> nil o.x.nil? #=> true o.x.?nil? #=> nil o.x.kind_of?(NilClass) #=> true o.x.?kind_of?(NilClass) #=> nil o.x.methods.include?(:nil?) #=> true ``` While it's arguably a bit peculiar to try to check that `nil` is `nil`, in a `nil`-safe way, `.?kind_of?(NilClass)` could reasonably return `true`. Also this is clearly a bit of a contrived example, it does highlight some of the more unexpected behaviors of the `.?` operator. Any chance of clarification? All best! Tom Reznick Software Engineer Continuity @threznick Nobuyoshi Nakada wrote: > Applied in changeset r52214. > > ---------- > Safe navigation operator > > * compile.c (iseq_peephole_optimize): peephole optimization for > branchnil jumps. > * compile.c (iseq_compile_each): generate save navigation operator > code. > * insns.def (branchnil): new opcode to pop the tos and branch if > it is nil. > * parse.y (NEW_QCALL, call_op, parser_yylex): parse token '.?'. > [Feature #11537] ---------------------------------------- Feature #11537: Introduce "Safe navigation operator" https://bugs.ruby-lang.org/issues/11537#change-54530 * 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/