From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on starla X-Spam-Level: X-Spam-Status: No, score=-1.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 Received: from nue.mailmanlists.eu (nue.mailmanlists.eu [94.130.110.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 955BC1F4CC for ; Tue, 7 Jan 2025 10:18:41 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; unprotected) header.d=ml.ruby-lang.org header.i=@ml.ruby-lang.org header.a=rsa-sha256 header.s=mail header.b=0yTnaIKH; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.a=rsa-sha256 header.s=s1 header.b=c06jmNuT; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1736245089; bh=KfzOzw8NSI2Iadrw4blMODXQSsbCPMoPg0VQpKCjdYg=; h=Date:References:To:Reply-To:Subject:List-Id:List-Archive: List-Help:List-Owner:List-Post:List-Subscribe:List-Unsubscribe: From:Cc:From; b=0yTnaIKHI+fu7+Lmeo3OzDEt4kg9KR5aOVph0xZizgD+d6Rbb8BkvYAy+9gigNQIo ZkH60yWt6O01Z+h6rqOwIOftUqkdUHTgNC5DPHdwd9Oq/7sbB8zaML8nk2RktbpokY c5ChHyO2OTc64x5/55rnAR8EgeQy6Y9R/Oqgf6Fg= Received: from nue.mailmanlists.eu (localhost [IPv6:::1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 1E9DE46673 for ; Tue, 7 Jan 2025 10:18:09 +0000 (UTC) Authentication-Results: nue.mailmanlists.eu; dkim=pass (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.a=rsa-sha256 header.s=s1 header.b=c06jmNuT; dkim-atps=neutral Received: from s.wfbtzhsw.outbound-mail.sendgrid.net (s.wfbtzhsw.outbound-mail.sendgrid.net [159.183.224.105]) by nue.mailmanlists.eu (Postfix) with ESMTPS id 3A8F244EC3 for ; Tue, 7 Jan 2025 10:18:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ruby-lang.org; h=from:references:subject:mime-version:content-type: content-transfer-encoding:list-id:to:cc:content-type:from:subject:to; s=s1; bh=IImVlOgS5Zeh1Q2YPVg8XNVQBmRbR7j80FZ0drLBJxo=; b=c06jmNuT27SY52LGOU2reH8tDrON/Xrmpt+JT+EFatLt0CksUEcA40nXLMGL7hfoyL5u wfhbJvlZOMB9K6vECP5Km7M/JuW5Lkuz149EHoDv7ZaDHAdIhxAltk7kFzJ+dwfH/xw78V yuBO0QNH3s3ufaxu3b2OmEvZn3CQb6NFw1aDTtOcJlcY2wZtGPPIN2OZm5mQeoeeVAdTLZ pdFwqqehLBY57+mtt4o0Vhm8uqyACMnkB8bBBYepvf7Pc1JDPJLK2oir5hqfubccka6Cxc r5RfAnjd/Lvwgr06ZIjGhsu//x349cinlxxV1cMY1pPGex/AMH3mxuZ/7yskJ44A== Received: by recvd-69cbc48b94-5dtvb with SMTP id recvd-69cbc48b94-5dtvb-1-677CFF5B-4 2025-01-07 10:18:03.168624147 +0000 UTC m=+4625641.396717601 Received: from herokuapp.com (unknown) by geopod-ismtpd-28 (SG) with ESMTP id Xrf3ogg6Sxikgbp7SUmh7g for ; Tue, 07 Jan 2025 10:18:03.127 +0000 (UTC) Date: Tue, 07 Jan 2025 10:18:03 +0000 (UTC) Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 21000 X-Redmine-Issue-Author: Dan0042 X-Redmine-Issue-Priority: Normal X-Redmine-Sender: mame 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-Redmine-MailingListIntegration-Message-Ids: 97195 X-SG-EID: =?us-ascii?Q?u001=2Ep+ckLDtT+4Y5c+H0YCkEnsuWiCQmn3OZA=2F9FzjoR6ZZlPaMv54M7EFoSM?= =?us-ascii?Q?CX5Trc79ep2R5F+0oYS4n23jq1cv8Px5uVgxxhx?= =?us-ascii?Q?h1gpo3CkxvW5ik9Fc4vdi11oJaoOEMB5JwApSJu?= =?us-ascii?Q?Xh8vNgmUlvxZC3t9mPOO1jBMX0JTOL5LPh5SLS3?= =?us-ascii?Q?dAciNyNm9DzEKgg3hvuTpG8KCrf4Cncms8tRqKr?= =?us-ascii?Q?Bf33myVqiSzeg+5rpyQKBcVlJ+LQljeqUZGPdy1?= =?us-ascii?Q?4Ot1?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: u001.I8uzylDtAfgbeCOeLBYDww== Message-ID-Hash: GFWB36MBNUS7TEKJAPYDB3QZ2JQ3TCJD X-Message-ID-Hash: GFWB36MBNUS7TEKJAPYDB3QZ2JQ3TCJD X-MailFrom: bounces+313651-b711-ruby-core=ml.ruby-lang.org@em5188.ruby-lang.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.9 Precedence: list Reply-To: Ruby developers Subject: [ruby-core:120519] [Ruby master Feature#21000] A way to avoid loading constant required by a type check List-Id: Ruby developers Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: "mame (Yusuke Endoh) via ruby-core" Cc: "mame (Yusuke Endoh)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #21000 has been updated by mame (Yusuke Endoh). When you create a ticket, please state clearly the problem you wish to solve. Here is my understanding. You see *two* problems with the following idiom ```ruby if defined?(Foo) and obj.is_a?(Foo) ``` * This idiom fires autoload. Before the actual load, there is no way that `obj` could be an instance of Foo, so we can tell "false" without firing autoload. * It is redundant to write the constant name twice. Therefore, you want a way to write a decision similar to this idiom without firing autoload. Preferably without writing the constant name twice. Is my understanding right? If so, how would your last proposal that `defined?(XYZ)` return `XYZ` solve this problem? Currently, for the constant `XYZ` for which autoload is set, `defined?(XYZ)` returns `"constant"` even before the actual load. If it were to return `XYZ` itself, I don't think it would solve the autoload problem. Or does your proposal involve changing `defined?(XYZ)` to return `nil` if it is before the actual load? I don't think that is a small incompatibility. ---------------------------------------- Feature #21000: A way to avoid loading constant required by a type check https://bugs.ruby-lang.org/issues/21000#change-111324 * Author: Dan0042 (Daniel DeLorme) * Status: Open ---------------------------------------- There is this pattern I encounter sometimes: ```ruby if defined?(NameSpace::ClassName) and obj.is_a?(NameSpace::ClassName) ``` Searching in gems, the pattern is fairly common: https://pastebin.com/VGfjRWNu I would like a way to avoid the repetition of `NameSpace::ClassName` above. I can think of a number of ways to approach the issue, each with different tradeoffs... ### Pattern match ignores uninitialized constant Pattern match like `obj in XYZ` could return false if XYZ is not defined. The danger here is that a typo could go undetected and just silently ignore the error even when the constants is expected to be defined. ### Pattern match has special syntax to ignore uninitialized constant Pattern match such as `obj in XYZ?` (or some other syntax) could return false if XYZ is not defined. The downside is that we're adding yet more new syntax. But it could be `obj in defined?(XYZ)` and then it doesn't really feel like new syntax. ### Do not autoload constants required by pattern match If we have `autoload :XYZ, "xyz"` then `obj in XYZ` could skip the autoload and return false. There is a possibility that `XYZ` might be defined as a regexp or other matcher that return true, but in general autoload is only used for classes/modules. And if the class/module is not yet loaded, obviously an object of that type cannot exist so we can avoid loading it. But this would only work for autoloaded constants, so can't be used to check a library that might not be loaded, ex: `obj in ActiveRecord::Base` ### defined?(mod) returns mod if it's a class/module If XYZ is a module, `defined?(XYZ)` could return XYZ instead of returning "constant". So it can be used in expressions like ```ruby case obj when nil when defined?(XYZ) ``` ```ruby if obj and defined?(XYZ) === obj ``` ```ruby if defined?(Gem::Specification)&.respond_to?(:each) ``` Very versatile, with the downside that it's a small backward incompatibiliy. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/