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,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 Received: from nue.mailmanlists.eu (nue.mailmanlists.eu [IPv6:2a01:4f8:1c0c:6b10::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id CFBFF1F44D for ; Mon, 26 Feb 2024 10:39:08 +0000 (UTC) Received: from nue.mailmanlists.eu (localhost [127.0.0.1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 7C4AD82C47; Mon, 26 Feb 2024 10:38:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1708943938; bh=/A5B8J30uFPJLmCD2yFuJ8Dc5ZT5Xz/C9Jt9/VTET0E=; 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=lSAJjvDkYelppLGFzabwyDPrfK2HzNpdeR1eS1/rXclreBoGMdUxjTwXrGsoqXCzZ IDv77mCqlb7dTbIJ2KfFeGcQhdKNYN7M6Ze5VK/xUBXvKcDTzc3xe/gqIAGjb9JZeH 3jypcdZmEBtfoTP1GUmSRv2x5658mK3Aff8gg0mk= Received: from s.csnrwnwx.outbound-mail.sendgrid.net (s.csnrwnwx.outbound-mail.sendgrid.net [198.37.146.154]) by nue.mailmanlists.eu (Postfix) with ESMTPS id 8C8DB82C40 for ; Mon, 26 Feb 2024 10:38:55 +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=gkRp5u+X; dkim-atps=neutral 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=EmNUWazXEujI8+4RlK+zvyY5ocJU4FYw0FubQI7LkmY=; b=gkRp5u+XhgPlXcxacNdp900zMe5n4DSaoYSpeGj3PIuKof8m/7+m2tI+vgckZ3Za3Htn wXrNjYvrYcU3tGTgfAWHl+fsCxYq8yrxjpju9thV8WEEKeguERoiplLhMW+ur2SkYdb8y2 whtq70zehAQh2E+52Uoqkfoj8U2FiSXQCRgTfJEWIWruCiRtXkUjZxRze+BmUsQPLV0MT1 M6tXa3dOl4/WVXW3DQbRNQLFPV2Y4Kk4CHX8IKA9S5UmRXHV+8oLQmJ59sk/DfhN1ge2YY bwmmNtS7RMgaFVhDDxjtOkHwb7BkNMrCAtHajgMAywo9ZUAIL5valyjlnYmu1lDw== Received: by filterdrecv-655bd866f5-cg5fx with SMTP id filterdrecv-655bd866f5-cg5fx-1-65DC6A3D-B 2024-02-26 10:38:53.451591677 +0000 UTC m=+1520528.807106801 Received: from herokuapp.com (unknown) by geopod-ismtpd-24 (SG) with ESMTP id ZFYAYLBOTkORRCsEd7-Rpw for ; Mon, 26 Feb 2024 10:38:53.442 +0000 (UTC) Date: Mon, 26 Feb 2024 10:38:53 +0000 (UTC) Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 20300 X-Redmine-Issue-Author: AMomchilov X-Redmine-Issue-Priority: Normal X-Redmine-Sender: rubyFeedback 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: 93504 X-SG-EID: =?us-ascii?Q?Q644LtTN96wPF7E6xvNEdWoop4IfLOVzD=2FriNwywGmeFmV7PHUiIcjqB3BHlIh?= =?us-ascii?Q?WL5WvLxyPh=2F7WYcsvB909cfRJ3qevImeO080XQs?= =?us-ascii?Q?=2Fn4uO70nv0ECGiqV62lRtpe36cGOW4fMJvdLOow?= =?us-ascii?Q?2klRxvYFpVRlYHUhT9ExVNco8ooYCh73xFFY5OQ?= =?us-ascii?Q?Co9r3FwawLTyr5In70Xp+qBxANiHgTBK4XjrjBx?= =?us-ascii?Q?LtpIKQatvJjBOazkqX=2Fi4QdGcnPg8gsYTvNhjM2?= =?us-ascii?Q?pjxVCt36tJ6rVNCS4X79g=3D=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== Message-ID-Hash: MEPMLNEICMQMZE3DK4YGOKRSZYOW6OS4 X-Message-ID-Hash: MEPMLNEICMQMZE3DK4YGOKRSZYOW6OS4 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.3 Precedence: list Reply-To: Ruby developers Subject: [ruby-core:116948] [Ruby master Feature#20300] Hash: set value and get pre-existing value in one call List-Id: Ruby developers Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: "rubyFeedback (robert heiler) via ruby-core" Cc: "rubyFeedback (robert heiler)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #20300 has been updated by rubyFeedback (robert heiler). Not so sure about these names. Note that get_and_set() may have people wonder why it is not set_and_get(). :D In regards to .update_value() people could ask why the old value is returned rather than the new one. I guess exchange_value is a bit more specific than the other variants, as people can say "I put in value x, as the new value, and get, in exchange, value y which was the old one". I don't have a better name suggestion though, but just from the alternatives so far I think nobu's suggestion is at the least better than the other ones, in my opinion. I don't have any particular opinion on the suggested functionality; in my own code I kind of keep setters and getters usually separate, or, e. g. first set and then get (or get first, assign to a variable, then set to a new value). It would be nice if we could have some kind of "universal idiom" for the desired functionality though - could make it easier if different programming languages, at the least OOP-centric ones, would use the same name/terminology for that. That would make naming things easier too, or at the least more consistent across programming languages, just like setters and getters are fairly well-established idioms in OOP-centric languages (ruby is kind of multi-paradigm; I don't know how functional programming languages solve this issue of setting and obtaining a new or old value in one go, so I can't comment on that). ---------------------------------------- Feature #20300: Hash: set value and get pre-existing value in one call https://bugs.ruby-lang.org/issues/20300#change-106988 * Author: AMomchilov (Alexander Momchilov) * Status: Open ---------------------------------------- When using a Hash, sometimes you want to set a new value, **and** see what was already there. Today, you **have** to do this in two steps: ```ruby h = { k: "old value" } # 1. Do a look-up for `:k`. old_value = h[:k] # 2. Do another look-up for `:k`, even though we just did that! h[:k] = "new value" use(old_value) ``` This requires two separate `Hash` look-ups for `:k`. This is fine for symbols, but is expensive if computing `#hash` or `#eql?` is expensive for the key. It's impossible to work around this today from pure Ruby code. One example use case is `Set#add?`. See https://bugs.ruby-lang.org/issues/20301 for more details. I propose adding `Hash#update_value`, which has semantics are similar to this Ruby snippet: ```ruby class Hash # Exact method name TBD. def update_value(key, new_value) old_value = self[key] self[key] = new_value old_value end end ``` ... except it'll be implemented in C, with modifications to `tbl_update` that achieves this with a hash-lookup. I'm opening to alternative name suggestions. @nobu came up with `exchange_value`, which I think is great. Here's a PR with a PoC implementation: https://github.com/ruby/ruby/pull/10092 ```ruby h = { k: "old value" } # Does only a single hash look-up old_value = h.update_value(:k, "new value") use(old_value) ``` -- 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/postorius/lists/ruby-core.ml.ruby-lang.org/