public inbox for systemtap@sourceware.org
 help / color / mirror / Atom feed
From: Craig Ringer <craig@2ndquadrant.com>
To: systemtap@sourceware.org
Subject: Safe-dereference operator?
Date: Fri, 17 Jul 2020 11:22:32 +0800	[thread overview]
Message-ID: <CAMsr+YG7sYTHJ_b2ped=AK7wCJygFkqGm8xBfEXeFnGyiL90jQ@mail.gmail.com> (raw)

Hi

TL;DR: Proposal to add a safe-dereference pseudo-operator like Groovy's ?.
to the language to simplify chasing pointer chains.

?. is basically an abbreviated ternary without the multiple-evaluation
hazard:

   a ?. b ?. c

is (assuming that all expressions are pure and side-effect free) the same
as:

   (!a ? 0 : (!a.b ? 0 : a.b.c))

i.e. this operator behaves like -> if the left operand is non-null. If the
left operand is null, it returns 0 and short-circuits out the right hand
expression. This is extremely useful when traversing chains of
possibly-null pointers.

Systemtap would possibly spell it as ?>,  ?->, or similar, since the
systemtap convention is to use "->" for all dereferences.

Rationale:

When writing systemtap code I routinely find myself writing logic to
dereference a chain of pointers and return the result, unless any pointer
on the chain is null, in which case a default is returned.

Say I want to

    return user_string($var->foo->bar)

I land up writing

ret = "";
foo = $var
if (foo != 0) {
   bar = @cast(foo, "Foo")->bar;
   if (bar != 0) {
     ret = user_string(bar);
   }
}
return ret

This is a tad tedious. Especially since systemtap does not track typeinfo
for locals, so you have to explicitly type everything.

It's possible to instead

try {
  return user_string($var->foo->bar)
} catch (ex) {
  return "";
}

but I'm not sure what performance implication that has, and more
importantly it may mask other unexpected errors that should not be caught
and swallowed.

A macro implementation is possible, but as noted above, prone to
multiple-eval hazards.

So - does this seem like something worth having in the language?

If so, would anyone here be willing to offer some advice on how feasible it
might be to implement, and where in the code I might want to start looking?

-- 
 Craig Ringer                   http://www.2ndQuadrant.com/
 2ndQuadrant - PostgreSQL Solutions for the Enterprise

             reply	other threads:[~2020-07-17  3:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-17  3:22 Craig Ringer [this message]
2020-08-18 23:04 ` Frank Ch. Eigler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAMsr+YG7sYTHJ_b2ped=AK7wCJygFkqGm8xBfEXeFnGyiL90jQ@mail.gmail.com' \
    --to=craig@2ndquadrant.com \
    --cc=systemtap@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).