cabbey,
@cabbey@phpc.social avatar

Does anyone have a better way to write the conditional calls to foo at lines 5-9 of this #php code? https://3v4l.org/fqCvv The goal is to have the default argument for bar trigger the default argument for foo, but they're different defaults (since one is nullable and the other isn't), and bar should not need to know what foo's default is. (obviously in my real code it's a much more complex signature, and separate classes.)

ramsey,
@ramsey@phpc.social avatar

@cabbey Maybe something like this in bar():

$i === null ? foo() : foo($i);

gmazzap,
@gmazzap@phpc.social avatar

@cabbey https://3v4l.org/s3fFk even if I'll probably just do this https://3v4l.org/5abr5

ramsey,
@ramsey@phpc.social avatar

@gmazzap @cabbey Your second example is what I was thinking, too.

cabbey,
@cabbey@phpc.social avatar

@ramsey @gmazzap yeah, as I discussed with crell in another branch of the thread the root issue was avoiding two calls to foo in a conditional with a long argument list. The solution I put into PR Friday assembles an argument array, conditionally adds the one discussed here then calls foo(…$args);

Crell,
@Crell@phpc.social avatar

@cabbey You're trying to avoid the null? You can use an enum to create an out of type default, that no person would ever pass in. That let's you differentiate between "nothing was specified" and "the same thing as the default was specified."

Though I may be misunderstanding you.

cabbey,
@cabbey@phpc.social avatar

@Crell honestly mostly trying to avoid having two copies of the call to foo() in an if/else with different argument lists. (way more obvious why in the real case with more params to foo.)
It really boils down to "this library allows you to specify an old format version and defaults to it's current one, which is an internal detail." vs "this function lets you specify the version of the object you're dealing with, in case you need to use an old object from storage"

Crell,
@Crell@phpc.social avatar

@cabbey Ah, that's tricky. In this simple case the enum trick could work if you put it on foo(). In a more complex case with multiple params, it's probably not worth it.

In theory you could use reflection on Foo to find its default value and pass that, but that's a stupid idea. :-)

cabbey,
@cabbey@phpc.social avatar

@Crell I was a Java developer for several years, I still cringe every time I resort to using reflection (all in tests, never in production code!) because of the memories of just how painful the performance penalties were back then.

I just started the PR with the "assemble an array and spread it" approach. I suspect it won't be received with torches and pitchforks.

Crell,
@Crell@phpc.social avatar

@cabbey Hm, that is actually a very reasonable idea. Especially in php8 with named args.

PHP reflection isn't as slow as Java, but still inappropriate here.

Girgias,
@Girgias@phpc.social avatar

@cabbey Use func_num_args()?

cabbey,
@cabbey@phpc.social avatar

@Girgias not sure I follow how that would help?

Girgias,
@Girgias@phpc.social avatar

@cabbey well you can use that function to determine if the call-site passed an argument or not, if not the default value is used in the function body.

It seems like you want different behaviour depending on what arity the function is called which the above function allows you to do e.g. https://3v4l.org/bFjSJ

cabbey,
@cabbey@phpc.social avatar

@Girgias ooh, yeah. I thought you meant down a level in foo. That's easy enough to determine if the flag is null that I don't think this is helpful. Also I'll note that in the real code, due to multiple named optional parameters it's a bit more complex: https://3v4l.org/PfBe4

Girgias,
@Girgias@phpc.social avatar

@cabbey sure for nullable types the null default works just fine.

I have opinions about multiple named parameters, but that's because so many terrible Python APIs are designed like that when they should effectively be multiple functions.

cabbey,
@cabbey@phpc.social avatar

@Girgias yeah, certain Obj-C library programmers really love that trick too. I recall an entire library for doing graphics that had functions like drawLine() which took 100 named variables, because you could specify points with x/y coords or with angle (as degrees or radians) and distance, or with a data class called a Point, then the other end could be specified with all sorts of different approaches including both absolute and relative offset from the start. Ugh. So glad doesn't do that!

  • All
  • Subscribed
  • Moderated
  • Favorites
  • php
  • DreamBathrooms
  • ngwrru68w68
  • modclub
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • InstantRegret
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • mdbf
  • GTA5RPClips
  • provamag3
  • ethstaker
  • normalnudes
  • tester
  • osvaldo12
  • everett
  • cubers
  • tacticalgear
  • anitta
  • megavids
  • Leos
  • cisconetworking
  • JUstTest
  • lostlight
  • All magazines