www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - substitute with predicate won't compile

reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
Why doesn't this compile?

```d
import std;

void main()
{
     string s;
     //...
     s = s.substitute!((a, b) => sicmp(a, b) == 0, "&amp;", "&",
                                                   "&lt;", "<",
                                                   "&gt;", ">",
                                                   "&quot;", `"`,
                                                   "&apos;", 
"'").array;
}
```

-- Bastiaan.
Oct 28
parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Tue, Oct 28, 2025 at 07:43:56PM +0000, Bastiaan Veelo via
Digitalmars-d-learn wrote:
 Why doesn't this compile?
 
 ```d
 import std;
 
 void main()
 {
     string s;
     //...
     s = s.substitute!((a, b) => sicmp(a, b) == 0, "&amp;", "&",
                                                   "&lt;", "<",
                                                   "&gt;", ">",
                                                   "&quot;", `"`,
                                                   "&apos;", "'").array;
 }
 ```
You have all your arguments as CT arguments; I think .substitute wants the lamdba as a CT argument but the other arguments as RT. But having said that, after changing the substitution arguments to RT arguments, it still doesn't compile for me for some reason. So I dunno. T -- They say that "guns don't kill people, people kill people." Well I think the gun helps. If you just stood there and yelled BANG, I don't think you'd kill too many people. -- Eddie Izzard, Dressed to Kill
Oct 28
parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Tuesday, 28 October 2025 at 20:08:41 UTC, H. S. Teoh wrote:
 On Tue, Oct 28, 2025 at 07:43:56PM +0000, Bastiaan Veelo via 
 Digitalmars-d-learn wrote:
 Why doesn't this compile?
 
 ```d
 import std;
 
 void main()
 {
     string s;
     //...
     s = s.substitute!((a, b) => sicmp(a, b) == 0, "&amp;", "&",
                                                   "&lt;", "<",
                                                   "&gt;", ">",
                                                   "&quot;", 
 `"`,
                                                   "&apos;", 
 "'").array;
 }
 ```
You have all your arguments as CT arguments; I think .substitute wants the lamdba as a CT argument but the other arguments as RT. But having said that, after changing the substitution arguments to RT arguments, it still doesn't compile for me for some reason. So I dunno. T
This seems to be a bug. It does work with runtime arguments, but only if the predicate is given as a string: ```d s = s.substitute!"a.toLower==b.toLower"("&amp;", "&", "&lt;", "<", "&gt;", ">", "&quot;", `"`, "&apos;", "'").to!string; ``` -- Bastiaan.
Oct 28
next sibling parent "H. S. Teoh" <hsteoh qfbox.info> writes:
On Tue, Oct 28, 2025 at 09:14:42PM +0000, Bastiaan Veelo via
Digitalmars-d-learn wrote:
[...]
 This seems to be a bug. It does work with runtime arguments, but only
 if the predicate is given as a string:
 ```d
     s = s.substitute!"a.toLower==b.toLower"("&amp;", "&",
                                             "&lt;", "<",
                                             "&gt;", ">",
                                             "&quot;", `"`,
                                             "&apos;", "'").to!string;
 ```
[...] Hmm. Probably a bug then! T -- What do you get if you throw a grenade into a French kitchen? Linoleum blown apart.
Oct 28
prev sibling parent Dukc <ajieskola gmail.com> writes:
On Tuesday, 28 October 2025 at 21:14:42 UTC, Bastiaan Veelo wrote:
 This seems to be a bug. It does work with runtime arguments, 
 but only if the predicate is given as a string:
 ```d
     s = s.substitute!"a.toLower==b.toLower"("&amp;", "&",
                                             "&lt;", "<",
                                             "&gt;", ">",
                                             "&quot;", `"`,
                                             "&apos;", 
 "'").to!string;
 ```

 -- Bastiaan.
Yes, I tried this and it seems to be the case. My error: ``` /nix/store/inkxb5vqy7agpm0wzqk07ha9a9llnfd2-dmd-2.107.1/include/dmd/std/algorith /iteration.d(6953): Error: function `app.main.substitute!((a, b) => a == b, string, string, string, string, string, string, string, string, string, string, string).substitute.SubstituteSplitter.popFront` cannot access function `find` in frame of function `D main` /nix/store/inkxb5vqy7agpm0wzqk07ha9a9llnfd2-dmd-2.107.1/include/dmd/std/algorith /searching.d(2388): `find` declared here app.d(8): Error: template instance `app.main.substitute!((a, b) => a == b, string, string, string, string, string, string, string, string, string, string, string)` error instantiating ``` I believe it means `substitute` is not correctly taking context pointers into account.
Oct 29