5 ms·
> Default parameters are far more complicated than method overloading. I've implemented both, I disagree. Unless you are talking about default arguments in th
by recursivedoubts 6y ago
> Default parameters are far more complicated than method overloading.
I've implemented both, I disagree. Unless you are talking about default arguments in the presence of method overloading, which is insane, and which I have also implemented.
- taeric 6y agoI'm taking about in use, mainly. Python, as an example, really screws it up by not giving you a way to know if you have the default or a value that equals it. Then, the more default values you pile in, the more this fun facet piles up. So, if you skip on that part of it, and you can while still getting far, it is easier. With it, though, is stupid complicated for little benefit.
- pietrovismara 6y agoWhy would you need to know if a value is the default or a value the equals the default? I can't think of a reason for that as I've never faced this problem in my career.
- taeric 6y agoIt is niche, but it does come up. Usually with boolean parameters, but also on migrations. Makes it possible to change defaults in a controlled way rather easily, since you can very easily log all of the places you used the default.
- tener 6y agoNah, you should be using another value if you care about that. For example in C# a nullable bool would be a fine choice (three values: true, false, null), or alternatively Option types.
- taeric 6y agoThis feels like classic blaming the user. :) You can also have better behavior between multiple defaults if you know whether someone gave a value or not. Basically, it is another tool compared to overloading. It should be no surprise that there can be preference on how to use between them.
- uryga 6y agoone example is `namedtuple`'s `._replace`: >>> x = Foo(a=1, b=2, c=3) >>> x._replace(a=1000, c=3000) Foo(a=1000, b=2, c=3000) in this case, you really need to know if the user passed a replacement or not.
- deleted 6y ago[deleted]
- dxdm 6y ago> Python, as an example, really screws it up by not giving you a way to know if you have the default or a value that equals it. It does: set a unique `object()` instance as the default. It will only compare equal to itself, but it's even clearer to read if you check for identity with `is`. You'll need to define it outside of the function definition. It's not the most concise, but it works, and it's an established pattern used in many high quality open source projects.
- taeric 6y agoThanks. This is kind of reinforcing my "not actually simple to use" point, though. :)
- tsimionescu 6y agoCommon Lisp has a pretty simple fix for this: when you declare a parameter with a default value, you can also declare a variable that will be true iff the parameter was actually passed. The function definition looks like this: (defun foo (&optional (a 1 a-passed)) (if a-passed (print a) (print "a not passed")) > (foo 10) 10 > (foo 1) 1 > (foo) a not passed This is still relatively easy to implement, and very easy to use in my opinion. Of course, combining this with named arguments is even better, and that is supported as well (just replace &optional with &key, and then specify the name when calling the function - (foo :a 1)).
- layer8 6y agoThe important thing with default parameters, if you have separate compilation, is that they are fixed in the callee and not in the caller. Otherwise, when the default value is changed, previously compiled callers will use a different value than declared by the callee. Compiling the default value into the caller is a problem in C++, and (IIRC) in Swift. In other words, (positional) default parameters should act like syntactic sugar for equivalent method overloads. And then actual overloads provide more flexibility for the callee implementation because it doesn’t have to represent the default case as a special in-band value.
- recursivedoubts 6y agoI can see that as an implementation detail, but it need not be one. There isn't a reason that a runtime couldn't use a flag value for absent args and let the callee code set the value up on their side. I mean, the java runtime has all sorts of crazy stuff going on w/ a security manager, to almost zero benefit. Seems easy enough to make something like this work.