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=0.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_BL_SPAMCOP_NET,SPF_HELO_PASS, SPF_PASS autolearn=no 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 400D01F4CC for ; Mon, 6 Jan 2025 09:40:54 +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=OOQ1T+qw; 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=I/kmrtHt; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1736156452; bh=VSoWInvq2oYfazckIKRx/qTNZNcPbYoDvAJ2xxyaaas=; 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=OOQ1T+qwIr/POthXi9djdcIZuNoDMb47WYPk6VFW/nguC2MRUh5k8eDv1nX48dZ7V K9MtIxD3wMBXfd99zRQmQIce94ZQjKxJg1Zl8CFKx692oiTiIXXcV4E8j8Sae6FWjZ 1gO5xmHg6GGEBNtXd48hwL9R+1YUx3VVsMiEfAes= Received: from nue.mailmanlists.eu (localhost [IPv6:::1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 317FC465AA for ; Mon, 6 Jan 2025 09:40:52 +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=I/kmrtHt; dkim-atps=neutral Received: from s.wrqvtvvn.outbound-mail.sendgrid.net (s.wrqvtvvn.outbound-mail.sendgrid.net [149.72.120.130]) by nue.mailmanlists.eu (Postfix) with ESMTPS id BFE7246530 for ; Mon, 6 Jan 2025 09:40:46 +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=ewpPyH1ijXVmuLXwmpa5/kyy/A4hD8RUFVOR9smFsD0=; b=I/kmrtHt2KBEpwpjPoSFNtIsGLBMqNH40jt8bZ2tW1l7tX98kjhRCG3iZXHLBtdC3oZS ZLb6W6MeW7ImDaL74xkn6GPt3qA6pw4Tj9FaAIB3ryrTk7212YmmjawukWfHgGOhCA0dBU vhG9jwdXIhNELABSf6uvImssCgEktmHO1a3bL3bQqtxIFHdJEoYVJcQjisSUP8JY9KZljy Zgk30Q2Snu6qGleXfAfhHBvyGpu05w4hOEl1zWVltAIR5fID4j8stqz1ICAMNCACTPSjy6 fCCirdpY6RFG0ycfbrB69Dl916FAxEN/HaUdiK6qTTtnX9Dj7BOyKkgTgo0hog1Q== Received: by recvd-786d47b7ff-vqwrg with SMTP id recvd-786d47b7ff-vqwrg-1-677BA51D-12 2025-01-06 09:40:45.569479233 +0000 UTC m=+4537087.281599584 Received: from herokuapp.com (unknown) by geopod-ismtpd-7 (SG) with ESMTP id lNjtQf6mS5yWgPyoF3iQSQ for ; Mon, 06 Jan 2025 09:40:45.517 +0000 (UTC) Date: Mon, 06 Jan 2025 09:40:45 +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: Eregon 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: 97172 X-SG-EID: =?us-ascii?Q?u001=2EByjZWvxTCjdoV8K03xEuhE7KqN4thWULFLM7+oH78KY30oYB3qFthsDpL?= =?us-ascii?Q?4w4cbYa3ttBh8bAHPOnE=2FkzPba67JNu7Lnrked2?= =?us-ascii?Q?O7K9VQ=2FJax1hNPuvQpOAZLbQFZWEfiPKNk4FAwF?= =?us-ascii?Q?vpFhimaHSqd2pUFSs0HNs+T4CFYCSlprORpYNu6?= =?us-ascii?Q?m85UYx+yR9ObERV7BqzZGlfXz2b8Y6fj90w6K4o?= =?us-ascii?Q?rJqM12Dq76EBbC0alQXW26WgHvtozCXGv9gyy6F?= =?us-ascii?Q?FZ4R2OHmqzRFPNaVtZgg8852eA=3D=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: u001.I8uzylDtAfgbeCOeLBYDww== Message-ID-Hash: 2EWCK2XYPM5LTAR7N6XRKTZZFEYFIMIU X-Message-ID-Hash: 2EWCK2XYPM5LTAR7N6XRKTZZFEYFIMIU 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:120496] [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: "Eregon (Benoit Daloze) via ruby-core" Cc: "Eregon (Benoit Daloze)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #21000 has been updated by Eregon (Benoit Daloze). `defined?` so far always returned `nil` or frozen strings, so I think it's not so great to use for this. Also `defined?` is particularly error-prone for constants & autoloading, for instance: ``` $DEBUG = true # to show swallowed exceptions autoload :Foo, "foo" p defined?(Foo) # nil p defined?(Foo::Bar) # Exception 'LoadError' at /home/eregon/.rubies/ruby-3.4.1/lib/ruby/3.4.0/rubygems.rb:1369 - cannot load such file -- rubygems/defaults/operating_system # Exception 'LoadError' at /home/eregon/.rubies/ruby-3.4.1/lib/ruby/3.4.0/rubygems.rb:1386 - cannot load such file -- rubygems/defaults/ruby # Exception 'LoadError' at :136 - cannot load such file -- foo # Exception 'LoadError' at :144 - cannot load such file -- foo nil ``` So `defined?(Foo::Bar)` will actually try to load `Foo`. ---------------------------------------- Feature #21000: A way to avoid loading constant required by a type check https://bugs.ruby-lang.org/issues/21000#change-111283 * 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) if obj and defined?(XYZ) === obj 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/