|
| 1 | +let |
| 2 | + /* |
| 3 | + About: Compare different optional parameter types |
| 4 | + This is from the post: https://ninmonkeys.com/blog/2024/06/05/power-query-functions-part1-using-optional-parameters |
| 5 | + |
| 6 | + lets call Text.Combine() to test declaring optional parameters |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + For this version: |
| 11 | + - you can pass a null value for a separator |
| 12 | + - always requires you pass 2 parameters |
| 13 | + */ |
| 14 | + Join_Nullable = (texts as list, separator as nullable text) => |
| 15 | + Text.Combine( texts, separator ), |
| 16 | + |
| 17 | + /* |
| 18 | + For this version: |
| 19 | + - you can pass a null value for a separator |
| 20 | + - you can skip the second parameter |
| 21 | + - 'optional' parameters are automatically 'nullable', |
| 22 | + so you can drop the 'nullable' part |
| 23 | + |
| 24 | + This is how library functions have multiple call signatures |
| 25 | + Power Query defines one function |
| 26 | + |
| 27 | + Other languages let you define multiple functions with shared name |
| 28 | + Based on the argument types, it'll call a different overloaded function |
| 29 | + */ |
| 30 | + Join_Optional = (texts as list, optional separator as text) => |
| 31 | + Text.Combine( texts, separator ), |
| 32 | + |
| 33 | + Summary = [ |
| 34 | + chars = { "a".."h" }, // example array of strings |
| 35 | + |
| 36 | + // this version lets you pass an explicit null value |
| 37 | + Nullable_1 = Join_Nullable( chars, ", " ), |
| 38 | + Nullable_2 = Join_Nullable( chars, null ), |
| 39 | + |
| 40 | + // but it requires you to pass something. It doesn't let you omit a parameter |
| 41 | + Nullable_3 = Join_Nullable( chars ), |
| 42 | + |
| 43 | + // this version lets you pass an explicit null value |
| 44 | + // or drop the parameter completely |
| 45 | + Join_Optional_1 = Join_Optional( chars, ", " ), |
| 46 | + Join_Optional_2 = Join_Optional( chars, null ), |
| 47 | + Join_Optional_3 = Join_Optional( chars ) |
| 48 | + ] |
| 49 | +in Summary |
0 commit comments