Re: Why do I have to cast arguments from int to byte?

2017-10-10 Thread rjframe via Digitalmars-d-learn
On Tue, 10 Oct 2017 19:55:36 +, Chirs Forest wrote:

> It wouldn't be so bad if I didn't have to use the word cast before each
> cast, bust since I have to specify both the word cast and the cast type
> and then wrap both the cast type and the value in brackets... it just
> explodes my code into multiple lines of unreadable mess.
> 
> 
> void foo(T)(T bar, T bar2, T bar3){...}
> 
> byte foobar = 12;
> 
> foo!byte(foobar + 1, foobar + 22, foobar + 333);
> vs.
> foo!byte(cast(byte)(foobar + 1), cast(byte)(foobar + 22),
> cast(byte)(foobar + 333));


You could wrap the cast in a function to clean it up a bit:

void main() {
byte foobar = 12;
foo!byte((foobar + 1).b, (foobar + 22).b, (foobar + 333).b);
}

byte b(int n) pure {
pragma(inline, true); // Probably not necessary.
return cast(byte)n;
}

void foo(T)(T bar, T bar2, T bar3) {
import std.stdio : writeln;
import std.string : format;
writeln("%s, %s, %s".format(bar, bar2, bar3));
}


--Ryan


Re: the best language I have ever met(?)

2017-10-10 Thread Igor Shirkalin via Digitalmars-d-learn

On Friday, 25 November 2016 at 19:16:43 UTC, ketmar wrote:
yeah. but i'm not Andrei, i don't believe that the only 
compiler task is to resolve templated code. ;-) i.e. Andrei 
believes that everything (and more) should be moved out of 
compiler core and done with library templates. Andrei is 
genius, for sure, but he is living somewhere in future, where 
our PCs are not bound by memory, CPU, and other silly 
restrictions. ;-)


tl;dr: using template for this sux.


Nice to meet you, Andrei!

Yes, in mathematics we are more servants than gentlemen (Charles 
Hermite).





Re: initializing a static array

2017-10-10 Thread kinke via Digitalmars-d-learn

On Tuesday, 10 October 2017 at 22:00:27 UTC, kinke wrote:

[...]


Ah sorry, overlooked that it's the initializer for a struct field.




Re: initializing a static array

2017-10-10 Thread kinke via Digitalmars-d-learn

On Tuesday, 10 October 2017 at 14:15:07 UTC, Simon Bürger wrote:
On Tuesday, 10 October 2017 at 13:48:16 UTC, Andrea Fontana 
wrote:

Maybe:

double[n] bar = 0.repeat(n).array;


This works fine, thanks a lot. I would have expected `.array` 
to return a dynamic array. But apparently the compiler is smart 
enough to know the length. Even the multi-dimensional case 
works fine:


double[n][n] bar = 0.repeat(n).array.repeat(n).array;


I hope this is not performance-critical code. The assembly is 
terrible for such code, at least for LDC, doing a GC allocation, 
unrolled reset to zero, then memcpying the dynamic array back to 
the stack: https://godbolt.org/g/uXBN75


`double[n] bar = void; bar[] = 0;` (2 lines, granted) results in 
a memset.


Re: Why do I have to cast arguments from int to byte?

2017-10-10 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 10 October 2017 at 19:55:36 UTC, Chirs Forest wrote:

Why?


D inherited a silly rule from C where any arithmetic is promoted 
to int first.


The big difference is D doesn't do implicit narrowing 
conversion... so x + 1 becomes int, but then int to byte requires 
an explicit cast (unless the compiler can prove the range in that 
particular expression - this is called "value range propagation").


I think it has proved to be a bit of a mistake :(


Re: how to shorten templates structs name?

2017-10-10 Thread Ali Çehreli via Digitalmars-d-learn

On 10/10/2017 04:30 AM, drug wrote:
> using classes I can make an inherited class of templated class and avoid
> too long mangled name:
> ```
> class TemplatedClass(A, Very, Much, Args, Here) { ... }
>
> class ShortenClass : TemplatedClass!(A,Very, Much, Args, Here) { ... };
> ```
> Now ShortenClass has a nice mangling.
>
> What can be done in case of struct?

I think an alias is more suitable here for both classes and structs:

alias ShortenClass = TemplatedClass!(int,bool, float, ubyte, string);

Ali



Re: Why do I have to cast arguments from int to byte?

2017-10-10 Thread Moritz Maxeiner via Digitalmars-d-learn

On Tuesday, 10 October 2017 at 19:55:36 UTC, Chirs Forest wrote:
I keep having to make casts like the following and it's really 
rubbing me the wrong way:


void foo(T)(T bar){...}

byte bar = 9;

[...]

Why?


Because of integer promotion [1], which is inherited from C.

[1] https://dlang.org/spec/type.html#integer-promotions


Re: Why do I have to cast arguments from int to byte?

2017-10-10 Thread Igor Shirkalin via Digitalmars-d-learn

On Tuesday, 10 October 2017 at 19:55:36 UTC, Chirs Forest wrote:
I keep having to make casts like the following and it's really 
rubbing me the wrong way:


void foo(T)(T bar){...}

byte bar = 9;

foo!byte(bar + 1); //Error: function foo!byte.foo (byte bar) is 
not callable using argument types (int)	

foo!byte(cast(byte)(bar + 1));

It wouldn't be so bad if I didn't have to use the word cast 
before each cast, bust since I have to specify both the word 
cast and the cast type and then wrap both the cast type and the 
value in brackets... it just explodes my code into multiple 
lines of unreadable mess.



void foo(T)(T bar, T bar2, T bar3){...}

byte foobar = 12;

foo!byte(foobar + 1, foobar + 22, foobar + 333);
vs.
foo!byte(cast(byte)(foobar + 1), cast(byte)(foobar + 22), 
cast(byte)(foobar + 333));


Why?


Because int (1) + ubyte (9) = int



Why do I have to cast arguments from int to byte?

2017-10-10 Thread Chirs Forest via Digitalmars-d-learn
I keep having to make casts like the following and it's really 
rubbing me the wrong way:


void foo(T)(T bar){...}

byte bar = 9;

foo!byte(bar + 1); //Error: function foo!byte.foo (byte bar) is 
not callable using argument types (int)	

foo!byte(cast(byte)(bar + 1));

It wouldn't be so bad if I didn't have to use the word cast 
before each cast, bust since I have to specify both the word cast 
and the cast type and then wrap both the cast type and the value 
in brackets... it just explodes my code into multiple lines of 
unreadable mess.



void foo(T)(T bar, T bar2, T bar3){...}

byte foobar = 12;

foo!byte(foobar + 1, foobar + 22, foobar + 333);
vs.
foo!byte(cast(byte)(foobar + 1), cast(byte)(foobar + 22), 
cast(byte)(foobar + 333));


Why?


Array/range-version of emplace

2017-10-10 Thread Nordlöw via Digitalmars-d-learn
Why isn't there an array/range version of `emplace`, when there 
is one for `moveEmplace`, namely


https://dlang.org/library/std/algorithm/mutation/move_emplace_all.html

?


Re: initializing a static array

2017-10-10 Thread ag0aep6g via Digitalmars-d-learn

On 10/10/2017 03:36 PM, Simon Bürger wrote:
I have a static array inside a struct which I would like to be 
initialized to all-zero like so


   struct Foo(size_t n)
   {
     double[n] bar = ... all zeroes ...
   }

(note that the default-initializer of double is nan, and not zero)

I tried

   double[n] bar = 0;  // does not compile


Works for me:


struct Foo(size_t n)
{
double[n] bar = 0;
}
void main()
{
import std.stdio;
Foo!5 foo;
writeln(foo.bar); /* prints "[0, 0, 0, 0, 0]" */
}



Re: Linking error: unresolved external symbol internal

2017-10-10 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/10/17 11:09 AM, MrSmith wrote:

I have a static library and an application.

When linking final executable I get:
     lib.lib(texteditor_1d_40c.obj) : error LNK2001: unresolved external 
symbol internal
     lib.lib(textbuffer_14_3ce.obj) : error LNK2001: unresolved external 
symbol internal


Happens on Windows 32 and 64 bit.
Only with debug build.
DMD 2.076.0
Error doesn't happen when I move code from library to the application.

Here is reduced test case (where 2 errors happen, 4 files): 
https://gist.github.com/MrSmith33/29125fa3538bb03637d0aebab6ccff7c
Here is smaller case (only 1 error, 2 files): 
https://gist.github.com/MrSmith33/dc53d8cb6ce642fcb6dbc5863d029cec



If this is relevant: 3 places I found in dmd where "internal" symbol is 
created:
* 
https://github.com/dlang/dmd/blob/4d86fcba2fd2ef86cc85738cd2ac2b059dbb5800/src/ddmd/backend/dt.c#L420 

* 
https://github.com/dlang/dmd/blob/4d86fcba2fd2ef86cc85738cd2ac2b059dbb5800/src/ddmd/tocsym.d#L662 

* 
https://github.com/dlang/dmd/blob/4d86fcba2fd2ef86cc85738cd2ac2b059dbb5800/src/ddmd/tocsym.d#L681 



No idea exactly what causes it, but here is another place I've seen it:

https://issues.dlang.org/show_bug.cgi?id=17740

-Steve


Re: initializing a static array

2017-10-10 Thread Daniel Kozak via Digitalmars-d-learn
https://run.dlang.io/is/SC3Fks


Linking error: unresolved external symbol internal

2017-10-10 Thread MrSmith via Digitalmars-d-learn

I have a static library and an application.

When linking final executable I get:
lib.lib(texteditor_1d_40c.obj) : error LNK2001: unresolved 
external symbol internal
lib.lib(textbuffer_14_3ce.obj) : error LNK2001: unresolved 
external symbol internal


Happens on Windows 32 and 64 bit.
Only with debug build.
DMD 2.076.0
Error doesn't happen when I move code from library to the 
application.


Here is reduced test case (where 2 errors happen, 4 files): 
https://gist.github.com/MrSmith33/29125fa3538bb03637d0aebab6ccff7c
Here is smaller case (only 1 error, 2 files): 
https://gist.github.com/MrSmith33/dc53d8cb6ce642fcb6dbc5863d029cec



If this is relevant: 3 places I found in dmd where "internal" 
symbol is created:
* 
https://github.com/dlang/dmd/blob/4d86fcba2fd2ef86cc85738cd2ac2b059dbb5800/src/ddmd/backend/dt.c#L420
* 
https://github.com/dlang/dmd/blob/4d86fcba2fd2ef86cc85738cd2ac2b059dbb5800/src/ddmd/tocsym.d#L662
* 
https://github.com/dlang/dmd/blob/4d86fcba2fd2ef86cc85738cd2ac2b059dbb5800/src/ddmd/tocsym.d#L681


Re: initializing a static array

2017-10-10 Thread Daniel Kozak via Digitalmars-d-learn
Yeah, you are right. My fault.

On Tue, Oct 10, 2017 at 4:47 PM, Adam D. Ruppe via Digitalmars-d-learn <
digitalmars-d-learn@puremagic.com> wrote:

> On Tuesday, 10 October 2017 at 14:42:15 UTC, Daniel Kozak wrote:
>
>> It will return dynamic array. it is same as:
>>
>> double[5] = [0,0,0,0,0]; // this is still dynamicaly allocated.
>>
>
> Not true here, the compiler knows it is going into a static array and puts
> the result directly in there. It handles literals.
>
> The range version though will allocate since the function doesn't know
> where its result ends up. It will CTFE though if the variable is static.
>


Re: initializing a static array

2017-10-10 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 10 October 2017 at 14:42:15 UTC, Daniel Kozak wrote:

It will return dynamic array. it is same as:

double[5] = [0,0,0,0,0]; // this is still dynamicaly allocated.


Not true here, the compiler knows it is going into a static array 
and puts the result directly in there. It handles literals.


The range version though will allocate since the function doesn't 
know where its result ends up. It will CTFE though if the 
variable is static.


Re: initializing a static array

2017-10-10 Thread Daniel Kozak via Digitalmars-d-learn
On Tue, Oct 10, 2017 at 4:15 PM, Simon Bürger via Digitalmars-d-learn <
digitalmars-d-learn@puremagic.com> wrote:

> On Tuesday, 10 October 2017 at 13:48:16 UTC, Andrea Fontana wrote:
>
>> On Tuesday, 10 October 2017 at 13:36:56 UTC, Simon Bürger wrote:
>>
>>> Is there a good way to set them all to zero? The only way I can think of
>>> is using string-mixins to generate a string such as "[0,0,0,0]" with
>>> exactly n zeroes. But that seems quite an overkill for such a basic task. I
>>> suspect I might be missing something obvious here...
>>>
>>
>> Maybe:
>>
>> double[n] bar = 0.repeat(n).array;
>>
>
> This works fine, thanks a lot. I would have expected `.array` to return a
> dynamic array. But apparently the compiler is smart enough to know the
> length. Even the multi-dimensional case works fine:
>
> double[n][n] bar = 0.repeat(n).array.repeat(n).array;
>

It will return dynamic array. it is same as:

double[5] = [0,0,0,0,0]; // this is still dynamicaly allocated.


Re: initializing a static array

2017-10-10 Thread Simon Bürger via Digitalmars-d-learn

On Tuesday, 10 October 2017 at 13:54:16 UTC, Daniel Kozak wrote:

struct Double
{
double v = 0;
alias v this;
}

struct Foo(size_t n)
{
Double[n] bar;
}


Interesting approach. But this might introduce problems later. 
For example `Double` is implicitly convertible to `double`, but 
`Double[]` is not implicitly convertible to `double[]`. Therefore 
I will stick with jmh530's solution for now, but thank you anyway.




Re: initializing a static array

2017-10-10 Thread Simon Bürger via Digitalmars-d-learn

On Tuesday, 10 October 2017 at 13:48:16 UTC, Andrea Fontana wrote:

On Tuesday, 10 October 2017 at 13:36:56 UTC, Simon Bürger wrote:
Is there a good way to set them all to zero? The only way I 
can think of is using string-mixins to generate a string such 
as "[0,0,0,0]" with exactly n zeroes. But that seems quite an 
overkill for such a basic task. I suspect I might be missing 
something obvious here...


Maybe:

double[n] bar = 0.repeat(n).array;


This works fine, thanks a lot. I would have expected `.array` to 
return a dynamic array. But apparently the compiler is smart 
enough to know the length. Even the multi-dimensional case works 
fine:


double[n][n] bar = 0.repeat(n).array.repeat(n).array;




Re: initializing a static array

2017-10-10 Thread Andrea Fontana via Digitalmars-d-learn

On Tuesday, 10 October 2017 at 13:53:37 UTC, jmh530 wrote:

double[n] bar;
bar[] = 0;


This works at runtime only for mutable arrays, anyway.



Re: initializing a static array

2017-10-10 Thread jmh530 via Digitalmars-d-learn

On Tuesday, 10 October 2017 at 13:48:16 UTC, Andrea Fontana wrote:


Maybe:

double[n] bar = 0.repeat(n).array;


Alt:

double[n] bar;
bar[] = 0;


Re: initializing a static array

2017-10-10 Thread Daniel Kozak via Digitalmars-d-learn
struct Double
{
double v = 0;
alias v this;
}

struct Foo(size_t n)
{
Double[n] bar;
}

Dne 10. 10. 2017 3:40 odpoledne napsal uživatel "Simon Bürger via
Digitalmars-d-learn" :

I have a static array inside a struct which I would like to be initialized
to all-zero like so

  struct Foo(size_t n)
  {
double[n] bar = ... all zeroes ...
  }

(note that the default-initializer of double is nan, and not zero)

I tried

  double[n] bar = 0;  // does not compile
  double[n] bar = {0}; // neither does this
  double[n] bar = [0]; // compiles, but only sets the first element,
ignoring the rest

Is there a good way to set them all to zero? The only way I can think of is
using string-mixins to generate a string such as "[0,0,0,0]" with exactly n
zeroes. But that seems quite an overkill for such a basic task. I suspect I
might be missing something obvious here...


Re: initializing a static array

2017-10-10 Thread Andrea Fontana via Digitalmars-d-learn

On Tuesday, 10 October 2017 at 13:36:56 UTC, Simon Bürger wrote:
Is there a good way to set them all to zero? The only way I can 
think of is using string-mixins to generate a string such as 
"[0,0,0,0]" with exactly n zeroes. But that seems quite an 
overkill for such a basic task. I suspect I might be missing 
something obvious here...


Maybe:

double[n] bar = 0.repeat(n).array;


Re: how to shorten templates structs name?

2017-10-10 Thread Daniel Kozak via Digitalmars-d-learn
Use alias this

Dne 10. 10. 2017 1:30 odpoledne napsal uživatel "drug via
Digitalmars-d-learn" :

> using classes I can make an inherited class of templated class and avoid
> too long mangled name:
> ```
> class TemplatedClass(A, Very, Much, Args, Here) { ... }
>
> class ShortenClass : TemplatedClass!(A,Very, Much, Args, Here) { ... };
> ```
> Now ShortenClass has a nice mangling.
>
> What can be done in case of struct?
>


initializing a static array

2017-10-10 Thread Simon Bürger via Digitalmars-d-learn
I have a static array inside a struct which I would like to be 
initialized to all-zero like so


  struct Foo(size_t n)
  {
double[n] bar = ... all zeroes ...
  }

(note that the default-initializer of double is nan, and not zero)

I tried

  double[n] bar = 0;  // does not compile
  double[n] bar = {0}; // neither does this
  double[n] bar = [0]; // compiles, but only sets the first 
element, ignoring the rest


Is there a good way to set them all to zero? The only way I can 
think of is using string-mixins to generate a string such as 
"[0,0,0,0]" with exactly n zeroes. But that seems quite an 
overkill for such a basic task. I suspect I might be missing 
something obvious here...


how to shorten templates structs name?

2017-10-10 Thread drug via Digitalmars-d-learn
using classes I can make an inherited class of templated class and avoid 
too long mangled name:

```
class TemplatedClass(A, Very, Much, Args, Here) { ... }

class ShortenClass : TemplatedClass!(A,Very, Much, Args, Here) { ... };
```
Now ShortenClass has a nice mangling.

What can be done in case of struct?


Re: Catching C++ Exceptions in D - Windows and Linux

2017-10-10 Thread Laeeth Isharc via Digitalmars-d-learn
On Tuesday, 12 September 2017 at 04:33:30 UTC, Nicholas Wilson 
wrote:
On Tuesday, 12 September 2017 at 03:51:45 UTC, Laeeth Isharc 
wrote:

Hi.

I'm here in HK with Ilya, Atila, John Colvin, and Jonathan 
Davis.
 I wondered what the current state of D catching C++ 
exceptions was on Linux and Windows.  I know that some work 
was done on making this possible, and my understanding is that 
it is, more or less - just wondered what the rough corners 
might be.


Thanks.


Laeeth.


IIRC I remember Walter saying that we should only bother to be 
able to catch C++ exceptions derived from std::exception, as 
opposed to say ints or strings. I believe the line was "those 
who throw ints should get what they deserve".


Apart from that I believe they should "just work", although I 
haven't tested.


OT I'm sorry I could make it to HK, I'm busy with uni all this 
week. I could possibly do telepresence though.


Nic


Thanks, Nic.  Sorry about the dates - was arranged last minute.  
I'll drop you a line shortly in any case.



Laeeth.



Re: How to call function with variable arguments at runtime?

2017-10-10 Thread Marc Schütz via Digitalmars-d-learn

On Tuesday, 10 October 2017 at 02:58:45 UTC, Mr. Jonse wrote:
I need to store a hetrogeneous array of delegates. How can I do 
this but still call the function with the appropriate number of 
parameters at run time?


I have the parameters as Variant[] params and a 
function/delegate pointer(void* for now).


Normally I'd push the parameters on the stack and use a call, 
but I'm sure D has some ability to do this, like apply(foo, 
args) would be the same as foo(args[0], ..., args[1]).


I'm not concerned about type correctness, it should always be 
consistent between what I call and what is stored.


Thanks.


Like so?

import std.variant;

void foo(int a, string b, float c) {
import std.stdio;
writefln("a = %s, b = %s, c = %s", a, b, c);
}

auto apply(alias fn)(Variant[] values) {
import std.traits : ParameterTypeTuple;
import std.conv : emplace;
alias Types = ParameterTypeTuple!fn;
assert(values.length == Types.length);
Types args = void;
foreach(i, ref arg; args) {
// using emplace instead of assignment here to be fully 
correct

emplace!(typeof(arg))(, values[i].get!(typeof(arg)));
}
return fn(args);
}

void main() {
Variant[] values = [Variant(1), Variant("Hello world"), 
Variant(3.14159f)];

apply!foo(values);
}


Re: How to call function with variable arguments at runtime?

2017-10-10 Thread bauss via Digitalmars-d-learn

On Tuesday, 10 October 2017 at 02:58:45 UTC, Mr. Jonse wrote:
I need to store a hetrogeneous array of delegates. How can I do 
this but still call the function with the appropriate number of 
parameters at run time?


I have the parameters as Variant[] params and a 
function/delegate pointer(void* for now).


Normally I'd push the parameters on the stack and use a call, 
but I'm sure D has some ability to do this, like apply(foo, 
args) would be the same as foo(args[0], ..., args[1]).


I'm not concerned about type correctness, it should always be 
consistent between what I call and what is stored.


Thanks.


Not entirely sure what you're wanting to do, but sounds a lot 
like variadic parameters.



https://dlang.org/spec/function.html#variadic mixed with some 
compile-time terminology.


Re: How to make commented code to compile?

2017-10-10 Thread bauss via Digitalmars-d-learn

On Monday, 9 October 2017 at 15:22:54 UTC, Adam D. Ruppe wrote:

On Monday, 9 October 2017 at 15:15:48 UTC, Zhuo Nengwen wrote:

test(cast(ushort) 1, (m, c) => {
  writeln(m);
  writeln(m);
});


Just remove the =>

(m, c) {
  // code here
}


Common mistake from people who worked with LINQ in C#.