Re: assert in unittest has access to private member?

2019-06-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, June 30, 2019 11:24:03 AM MDT Robert M. Münch via Digitalmars-d-
learn wrote:
> I have a case, with templates, where an assert in a unittest can access
> a private memember and I don't know how this can happen.
>
> Before trying to creat an equivalent case, I want to cross-check, if
> assert has special semantics in a unittest so that it can access
> private memembers?

I know that there at least used to be a bug where templates were treated as
public instead of private, though I thought that that was fixed some time
ago. Regardless, as the others have pointed out, private in D is private to
the module, not the class or struct or template or whatever. So, if your
unittest block is in the same module as what you're testing, it legitimately
has access to all private members, and that's not a bug. On the other hand,
if your unittest block is in another module from the declaration, and it's
accessing a private member, that that is a bug unless you're using template
mixins to mix the code into the current module.

D treats the module as the unit of encapsulation so that it doesn't have to
worry about having features like C++'s friend, and in general, this has
worked out extremeley well, though it tends to surprise many people at
first, since many (most?) don't read the documentation carefully enough and
assume that private is private to the class as it is in many other
languages:

https://dlang.org/spec/attribute.html#visibility_attributes

Certainly, it's great in general that unittest blocks can access private in
the same module, because it makes it easy to test private functions without
having to alter the API of your class like you would with something like
JUnit in Java. If for some reason, you really do need something to not have
access to private members, then you need to put it in a separate module.

- Jonathan M Davis






Re: assert in unittest has access to private member?

2019-06-30 Thread XavierAP via Digitalmars-d-learn

On Sunday, 30 June 2019 at 17:24:03 UTC, Robert M. Münch wrote:
I have a case, with templates, where an assert in a unittest 
can access a private memember and I don't know how this can 
happen.


Modules are the units of encapsulation in D:

https://dlang.org/spec/attribute.html#visibility_attributes


Re: assert in unittest has access to private member?

2019-06-30 Thread a11e99z via Digitalmars-d-learn

On Sunday, 30 June 2019 at 17:24:03 UTC, Robert M. Münch wrote:
I have a case, with templates, where an assert in a unittest 
can access a private memember and I don't know how this can 
happen.


Before trying to creat an equivalent case, I want to 
cross-check, if assert has special semantics in a unittest so 
that it can access private memembers?


Private means that only members of the enclosing class can access 
the member, or

vvv
members and functions in the same module as the enclosing class.
^^^
did you take it into account?


assert in unittest has access to private member?

2019-06-30 Thread Robert M. Münch via Digitalmars-d-learn
I have a case, with templates, where an assert in a unittest can access 
a private memember and I don't know how this can happen.


Before trying to creat an equivalent case, I want to cross-check, if 
assert has special semantics in a unittest so that it can access 
private memembers?


--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Re: Finding the maxElement of Two-Dimensional Array

2019-06-30 Thread Samir via Digitalmars-d-learn

On Sunday, 30 June 2019 at 15:38:42 UTC, a11e99z wrote:

try to take slice from static arrays
writeln(ta[1][].maxElement);


That does what I am looking for.  Thank you for the quick reply!


Re: Finding the maxElement of Two-Dimensional Array

2019-06-30 Thread a11e99z via Digitalmars-d-learn

On Sunday, 30 June 2019 at 15:22:42 UTC, Samir wrote:

How come this works:
int[4][2] ta = [[2, 1, 4, 3], [3, 10, 2, 5]];
writeln(ta[1].maxElement); // get error on this line



try to take slice from static arrays
writeln(ta[1][].maxElement);
or use dynamic arrays (slices)
int[][] ta = [[2, 1, 4, 3], [3, 10, 2, 5]]; // or auto ta



Finding the maxElement of Two-Dimensional Array

2019-06-30 Thread Samir via Digitalmars-d-learn

How come this works:

int[][] ta = [[2, 1, 4, 3], [3, 10, 2, 5]];
writeln(ta[1].maxElement); // 10

but I get an error when specifying the number of elements when 
declaring the array:


int[4][2] ta = [[2, 1, 4, 3], [3, 10, 2, 5]];
writeln(ta[1].maxElement); // get error on this line

Error: template std.algorithm.searching.maxElement cannot deduce 
function from argument types !()(int[4]), candidates are:

/home/samir/dlang/dmd-2.082.0/freebsd/bin64/../../src/phobos/std/algorithm/searching.d(3576):
std.algorithm.searching.maxElement(alias map = (a) => a, Range)(Range r) if 
(isInputRange!Range && !isInfinite!Range)
/home/samir/dlang/dmd-2.082.0/freebsd/bin64/../../src/phobos/std/algorithm/searching.d(3583):  
  std.algorithm.searching.maxElement(alias map = (a) => a, Range, RangeElementType = 
ElementType!Range)(Range r, RangeElementType seed) if (isInputRange!Range && 
!isInfinite!Range && !is(CommonType!(ElementType!Range, RangeElementType) == void))

Thanks
Samir