@@ -95,28 +95,68 @@ NotFoundLookupError: `g(1, 'b')` could not be resolved...
95
95
(why)=
96
96
## Why Doesn't Dispatch Fully Support Keyword Arguments?
97
97
98
- In multiple dispatch, a function can have many implementations, called methods.
99
- For all methods of a function, what is meant by the first argument is unambiguous and
100
- clear.
101
- However, what is meant by an argument named ` x ` depends on where a method
102
- positioned ` x ` :
103
- for some methods, ` x ` might be the first argument, whereas for other method ` x `
104
- might be the second argument.
105
- In general, for a function with many methods, argument ` x ` does not have a unique
106
- position.
107
- In other words, for functions with many methods,
108
- there is usually no correspondence between argument names and positions.
109
-
110
- We therefore see that
111
- supporting both positional and named arguments hence results in a specification that
112
- mixes two non-corresponding systems.
113
- Whereas this would be possible, and admittedly it would be convenient to support named
114
- arguments, it would add substantial complexity to the dispatch process.
115
- In addition, for named arguments to be usable,
116
- it would require all methods of a function
117
- to name their arguments in a consistent manner.
118
- This can be particularly problematic if the methods of a function are spread across
98
+ It would technically be possible to dispatch of keyword arguments.
99
+ Whereas Plum should or not is an ongoing discussion.
100
+
101
+ The main argument against is that dispatching on keyword arguments
102
+ would make the dispatch process sensitive to argument names.
103
+ For this to work well, the arguments of all methods of a function would
104
+ have to be named consistently.
105
+ This can be problematic if the methods of a function are spread across
119
106
multiple packages with different authors and code conventions.
107
+ In contrast, dispatching only on positional arguments means that
108
+ dispatch does not depend on argument names.
120
109
121
- In general, Plum closely mimics how multiple dispatch works in the
110
+ In general, Plum attempts to mimics how multiple dispatch works in the
122
111
[ Julia programming language] ( https://docs.julialang.org/en/ ) .
112
+
113
+ ## I Really Want Keyword Arguments!
114
+
115
+ You can use the following pattern as a work-around,
116
+ which converts all arguments to positional arguments using a wrapper function:
117
+
118
+ ``` python
119
+ from plum import dispatch
120
+
121
+
122
+ def f (x = None , y = None ):
123
+ return _f(x, y)
124
+
125
+
126
+ @dispatch
127
+ def _f (x : int , y : None ):
128
+ print (" Only `x` is provided! It is an integer." )
129
+
130
+
131
+ @dispatch
132
+ def _f (x : float , y : None ):
133
+ print (" Only `x` is provided! It is a float." )
134
+
135
+
136
+ @dispatch
137
+ def _f (x : None , y : float ):
138
+ print (" Only `y` is provided! It is a float." )
139
+
140
+
141
+ @dispatch
142
+ def _f (x : int , y : float ):
143
+ print (" Both are provided!" )
144
+ ```
145
+
146
+ ``` python
147
+ >> > f(x = 1 )
148
+ Only `x` is provided! It is an integer.
149
+
150
+ >> > f(x = 1.0 )
151
+ Only `x` is provided! It is a float .
152
+
153
+ >> > try : f(y = 1 )
154
+ ... except Exception as e: print (f " { type (e).__name__ } : { e} " )
155
+ NotFoundLookupError: `_f(None , 1 )` could not be resolved...
156
+
157
+ >> > f(y = 1.0 )
158
+ Only `y` is provided! It is a float .
159
+
160
+ >> > f(x = 1 , y = 1.0 )
161
+ Both are provided!
162
+ ```
0 commit comments