Code duplication where you wish to have a routine called with either immutable or mutable arguments

Cecil Ward cecil at cecilward.com
Wed May 31 03:23:01 UTC 2023


On Tuesday, 30 May 2023 at 04:15:22 UTC, Ali Çehreli wrote:
> On 5/29/23 19:57, Cecil Ward wrote:
>
> > I wish to have one routine
> > that can be called with either immutable or (possibly)
> mutable argument
> > values.
>
> 'const' should take both immutable and mutable. Can you show 
> your case with a short example?
>
> > Could I make the one routine into a template?
>
> That could work but give 'in' parameters a try:
>
>   https://dlang.org/spec/function.html#in-params
>
> Ali

T2 foo( in T1 x ) { return bar( x ) };

It was with something vaguely like the above that I had to remove 
the in (templatised generic function possibly) in order to get it 
to compile with GDC or LDC on godbolt.org (or d.godbolt.org) 
latest versions available. -O3 -release/-frelease 
-march=native/-mcpu-native




More information about the Digitalmars-d-learn mailing list