News for hackers. Stuff that matters.
Re:On Learning Rust and Go: Migrating Away from Python by newen on Sunday March 24, 2019 @09:16AM (#19476158)
Why would you ever do that? Why would you even want a function that accepts either a file name or file contents when both are strings? What's so hard about defining a new function?
Re:On Learning Rust and Go: Migrating Away from Python by mjw1007 on Sunday March 24, 2019 @09:42AM (#19476283)
One common case where this is attractive is where you have a bunch of "public" functions built on top of each other, with the higher ones passing that parameter on and not otherwise caring what it is:

  high_level_function(src, ...)
      ...
      mid_level_function(src, ...)
      ...

  mid_level_function(src, ...)
      low_level_function(src, ...)
      ...

  low_level_function(src, ...)
      ...
If you can make the low-level function generic one way or another, you avoid having to duplicate the higher-level functions.

Another case is where there are two or more parameters that you want to be generic in this sense: if your only option is duplicating the function you can end up having to define inconveniently many variants.

Re:On Learning Rust and Go: Migrating Away from Python by newen on Sunday March 24, 2019 @10:45AM (#19476619)
Yeah, that is convenient and I do that often. But I try to wrap the variables in different types depending on their source (e.g. whether it's a filename or file content) so that functions further down can disambiguate between variables explicitly.
Re:On Learning Rust and Go: Migrating Away from Python by guidopallemans on Sunday March 24, 2019 @09:52AM (#19476327)
Re:On Learning Rust and Go: Migrating Away from Python by makapuf on Sunday March 24, 2019 @10:34AM (#19476555)
Why not have clearer file, filename or data keyword arguments ? Or just file and call open or stringio if you have the file name or data ? Plenty of more explicit solutions.
Re:On Learning Rust and Go: Migrating Away from Python by joshvm on Tuesday March 26, 2019 @01:10PM (#19493808)
Lots of public APIs support this behaviour, it's convenient for users. For example numpy.fromfile will accept a file object or a string.