`with` across function calls

Nicholas Wilson iamthewilsonator at hotmail.com
Sat Jan 19 01:35:41 UTC 2019


On Saturday, 19 January 2019 at 00:51:28 UTC, luckoverthere wrote:
> On Saturday, 19 January 2019 at 00:23:40 UTC, Nicholas Wilson 
> wrote:
>> How do you know? I don't think it would be all that much more 
>> complex than default arguments.
>
> Default arguments don't change. In every context you look the 
> function will behave the same way no matter where it is used. 
> It'd be no different than adding an overload. With the with 
> statement though, it'd not obvious where it might be used.

Yes they can: https://run.dlang.io/is/IEQb0O
This would be like that, except: a) without the global, and b) 
you specifying that you want to use `a` where a implicit int 
parameter is expected.

> You can't grep or find because the with statement removes it 
> all entirely.

Uh, yes you can: "grep with", it will show up in the function 
signature _and_ in the scope of the call site. Its like being 
able to grep for "cast" (which is literally the entire reason for 
having cast as a keyword).

> That's sort of my point, you'd have to look at every function 
> definition to see which ones get modified. At the call point 
> you have no indication that the behavior is different.

Again this is not different to default parameters, also you have 
a `with (foo)` in scope.

> It's not the same as default arguments. You can tell at call 
> point the number of parameters being passed to the function, 
> and the behavior is the same everywhere that function is used. 
> The two are absolutely different.

What? Firstly with default parameters you _can't_ tell how many 
parameters are actually passed to the function at the call site. 
Secondly, this is _exactly_ the same as default parameters. But 
also like default parameters, you typically don't care because 
they are default for a reason i.e. the defaults make sense.

> Why did you split my sentence in half? You agree with me in the 
> reply to the example given with this sentence.

Because there were two halves to the sentence.

>> The case of dealing with contexts is hardly unique to me.
> Contexts is the specific use case, not you.

Contexts are the general use case, yes. I'm not sure what you're 
point is there.

> Even if you were to use a global or this new proposed "with" 
> feature, that function call is atrocious.

Welcome to the wonderful world of compute API, and believe me, 
this is nice. That single call in OpenCL is 4 + O(arg_set2) 
calls, CUDA its about 4 and they are all horrible and involve no 
end of casts to void* and is as type unsafe as its is possible 
be. The fact that it is a single call and type safe speaks 
wonders for D's meta programming.



More information about the Digitalmars-d mailing list