Re: Workaround to support default parameter values

Henrik Grubbström <[email protected]> Thu, 20 Mar 2025 19:59:55 +0100 (CET)
Newsgroups gmane.comp.lang.pike.user
Organization Roxen Internet Software AB
Message-ID <[email protected]>
  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-658135633-1742497195=:20628
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8BIT

On Thu, 20 Mar 2025, Marcos Cruz wrote:

> Since Pike does not support default parameter values, I use this
> workaround:
>
> ```pike
> int typed_number(string|void prompt) {
>    if (!stringp(prompt)) {
>        prompt = "";
>    }
>    // (…)
> }
> ```
>
> Is there a better approach?

In Pike 9.0 (currently in beta) the following syntax is supported:

```pike
int typed_number(string prompt = "") {
    // (…)
}
```

It will compile to code similar to

```pike
int typed_number(string|void prompt) {
    prompt = prompt || "";
    // (…)
}
```

Except that the variable `prompt` will have the type
`string` and not `string|void`.

Did this help?

 	/grubba

-- 
Henrik Grubbström					[email protected]
Roxen Internet Software AB
--8323328-658135633-1742497195=:20628--