`with` across function calls

luckoverthere luckoverthere at gmail.cm
Sat Jan 19 22:55:49 UTC 2019


On Saturday, 19 January 2019 at 01:35:41 UTC, Nicholas Wilson 
wrote:
> 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.

That's pretty awful too, let's not propagate that any further 
with with. But not really what I mean, at the very least with 
that default value you know that function access a global for 
some reason, and should probably be avoided.

>> 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).

Lol I can only imagine this, does anyone actually even grep. I 
wonder just how many hits doing grep __traits does with something 
like phobos. This is by no means scalable at all. When reading 
code no one wants to have to grep around the entire code base 
because someone was lazy.

>> 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.

Yes you have with in scope, but that's it. You'd have to look 
through every function in that with scope to see which functions 
are affected by it. In the event something is wrong.

>> 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.

It is NOT _exactly_ the same, irregardless of where you use a 
function with default values they are going to behavior the same 
no matter where they.

void test( int a, int b = 10 );


test( 10 );     // same behavior *everywhere*
test( 10, 10 ); // same behavior *everywhere*

with( codeSmell ) {
     test( 10 );     // same behavior *everywhere*
     test( 10, 10 ); // same behavior *everywhere*
}

static this() {
     test( 10 );     // same behavior *everywhere*
     test( 10, 10 ); // same behavior *everywhere*
}


Where as with your proposed DIP.


void test( int a = with );

test(); // nope can't do t his

int value;
with( value ) {
     test(); // ok different behavior given context, unexpected
}

This is honestly why I don't use with() basically at all. And 
neither does anyone else really. It just makes logic harder to 
reason about.


>> 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.

And you misunderstood both halves because you didn't treat them 
as a whole :).

>>> 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.

They are a specific use case yes.

>> 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.

Yikes, I wouldn't be celebrating making that a single call. 
Especially since you are effectively passing 3 different sets of 
arguments in one line of code. That's not something to be 
celebrating.

Either way good luck getting this DIP passed, you'll need it.



More information about the Digitalmars-d mailing list