Re: Raw Binary into the console

2014-07-11 Thread Ali Çehreli via Digitalmars-d-learn

On 07/11/2014 10:56 PM, Sean Campbell wrote:

> How Can I Print Raw Binary Into The Console?

Taking a step back, a D program prints to stdout, not console. However, 
in most cases a D program's output ends up on the console when it is 
started in a console.


So, one can print any byte value to the stdout, which usually ends up on 
the console. Now, the question is how that value will be interpreted by 
the console. On some consoles (e.g. Linux) the byte stream is 
interpreted as a UTF-8 stream or a control character in the case of 0b011.


> I Have Variable Of Type ubyte which equals 0b011 How Can I Get The
> Variable To Print Out As 011

That is different though. You seem to want to print the value 0b011 as 
the string "011". %b format specifier does that. However, in most cases 
it is also useful to zero-pad the output:


writefln("%08b", 0b011);

Ali



Raw Binary into the console

2014-07-11 Thread Sean Campbell via Digitalmars-d-learn

How Can I Print Raw Binary Into The Console?
I Have Variable Of Type ubyte which equals 0b011 How Can I Get 
The Variable To Print Out As 011


Re: I don't get it. version(unittest) can't seem to use local variable

2014-07-11 Thread Ali Çehreli via Digitalmars-d-learn

On 07/11/2014 10:08 PM, dysmondad wrote:

> class Velocity
> {

[...]

>  ref Velocity opOpAssign(string op) ( in float multiplier )

Unrelated to your question, you want to return just Velocity there. 
Unlike C++, classes are reference types in D. So, Velocity itself is 
essentially a Velocity* in C++.


Ali



Re: I don't get it. version(unittest) can't seem to use local variable

2014-07-11 Thread Rikki Cattermole via Digitalmars-d-learn

On 12/07/2014 5:08 p.m., dysmondad wrote:

I'm new to D. I've been using C since it was a baby and C++ back when it
was only a pre-compiler for c. So, I may just be stuck thinking in C land.

The issue is I am attempting to use the version(unittest) feature.
However the compiler pukes up the error below. I'm sure it's something
easy enough but I've not been able to find any help in Google land. I
don't know if I have the opOpAssign defined correctly or if there's
something wrong with the constructor or both.

dysmondad@Julep:~/src/D$ gdc -c velocity.d
velocity.d:110: error: no identifier for declarator v
velocity.d:110: error: semicolon expected, not '*='
velocity.d:110: error: Declaration expected, not '*='


class Velocity
{
private:

 float _Y = 0.0;
 float _X = 0.0;

public:
 this (float x, float y )
 {
 _Y = y;
 _X = x;
 }

 ref Velocity opOpAssign(string op) ( in float multiplier )
 if( op == "*" )
 {
 _Y *= multiplier;
 _X *= multiplier;
 return this;
 }
}

version(unittest)
{
 Velocity v = new Velocity( 2.0f, 5.0f );
 v *= 5.0f;  // <- line 110
 printf( "v = %s\n", to!string(v) );
}


try:

unittest {
  Velocity v = new Velocity( 2.0f, 5.0f );
  v *= 5.0f;  // <- line 110
  printf( "v = %s\n", to!string(v) );
}

instead. Basically version is like static if, it doesn't indicate its a 
function. Instead it changes what code gets compiled.
Where as a unittest block, is essentially just a function that gets 
called at a special time, that is only compiled as if version(unittest) 
was also used.


I don't get it. version(unittest) can't seem to use local variable

2014-07-11 Thread dysmondad via Digitalmars-d-learn
I'm new to D. I've been using C since it was a baby and C++ back 
when it was only a pre-compiler for c. So, I may just be stuck 
thinking in C land.


The issue is I am attempting to use the version(unittest) 
feature. However the compiler pukes up the error below. I'm sure 
it's something easy enough but I've not been able to find any 
help in Google land. I don't know if I have the opOpAssign 
defined correctly or if there's something wrong with the 
constructor or both.


dysmondad@Julep:~/src/D$ gdc -c velocity.d
velocity.d:110: error: no identifier for declarator v
velocity.d:110: error: semicolon expected, not '*='
velocity.d:110: error: Declaration expected, not '*='


class Velocity
{
private:

float _Y = 0.0;
float _X = 0.0;

public:
this (float x, float y )
{
_Y = y;
_X = x;
}

ref Velocity opOpAssign(string op) ( in float multiplier )
if( op == "*" )
{
_Y *= multiplier;
_X *= multiplier;
return this;
}
}

version(unittest)
{
Velocity v = new Velocity( 2.0f, 5.0f );
v *= 5.0f;  // <- line 110
printf( "v = %s\n", to!string(v) );
}


Re: Insert a char in string

2014-07-11 Thread JR via Digitalmars-d-learn

On Thursday, 10 July 2014 at 19:33:15 UTC, simendsjo wrote:

On 07/10/2014 06:05 PM, Alexandre wrote:

I have a string X and I need to insert a char in that string...

auto X = "100";

And I need to inser a ',' in position 3 of this string..., I 
try to use

the array.insertInPlace, but, not work...

I try this:
auto X = "100";
auto N = X.insertInPlace(1,'0');


Do you really want to insert a comma in the string, or do you 
want to

format a number as "100,000,000,000.00"?


For that, one approach would be as in 
http://dpaste.dzfl.pl/bddb71eb75bb.


Re: Question about @nogc in D 2.066

2014-07-11 Thread Dicebot via Digitalmars-d-learn

On Friday, 11 July 2014 at 21:02:30 UTC, Timon Gehr wrote:
He is allocating an immutable(int)[] here. There is no reason 
why it should allocate unless providing different addresses for 
different runs of the code is considered a feature, as the 
literal has a compile-time known value. It's just that the 
frontend does not actually elide the allocation, and this would 
be a valid extension to ask for.


In any case, the allocation can be manually elided as follows:

void main(string[] args)@nogc{
auto s1 = "hello";
static immutable _a1 = [1, 2];
auto a1 = _a1;
}

(Not actually tested, because I am having trouble downloading 
the beta for some reason.)


Sure, no objections here - it just happened that doing so for 
string literals was more obvious because literals themselves are 
immutable. And doing so for arrays requires tiny bit of 
semantical analysis (probably not really difficult too, I don't 
know)


Re: Question about @nogc in D 2.066

2014-07-11 Thread Trass3r via Digitalmars-d-learn

http://forum.dlang.org/thread/pxotrowaqcenrpnnw...@forum.dlang.org


Re: Question about @nogc in D 2.066

2014-07-11 Thread Timon Gehr via Digitalmars-d-learn

On 07/11/2014 10:39 PM, Dicebot wrote:

Key difference is that type of string literal is immutable(char)[] so it
is perfectly legal to keep it in binary text segment. Type of array
literal is just T[] (int[] here) and you can possibly mutate their
elements. Because of this each assignment of array literal needs to
allocate a new copy contrary to immutable strings which can all
reference same memory chunk.


He is allocating an immutable(int)[] here. There is no reason why it 
should allocate unless providing different addresses for different runs 
of the code is considered a feature, as the literal has a compile-time 
known value. It's just that the frontend does not actually elide the 
allocation, and this would be a valid extension to ask for.


In any case, the allocation can be manually elided as follows:

void main(string[] args)@nogc{
auto s1 = "hello";
static immutable _a1 = [1, 2];
auto a1 = _a1;
}

(Not actually tested, because I am having trouble downloading the beta 
for some reason.)


Re: Question about @nogc in D 2.066

2014-07-11 Thread Dicebot via Digitalmars-d-learn
Key difference is that type of string literal is 
immutable(char)[] so it is perfectly legal to keep it in binary 
text segment. Type of array literal is just T[] (int[] here) and 
you can possibly mutate their elements. Because of this each 
assignment of array literal needs to allocate a new copy contrary 
to immutable strings which can all reference same memory chunk.


Re: Question about @nogc in D 2.066

2014-07-11 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 11 July 2014 at 20:02:32 UTC, Weasel wrote:

Why does the s1 not throw an error, but the a1 does?


Strings don't allocate upon use whereas all other arrays do 
unless you specifically mark it as static - immutability isn't 
considered here (I think because the part of the compiler that 
looks at the array literal doesn't look at the type on the left 
hand side so it just doesn't know but i'm not sure about why).


But if you make the immutable other array static it shouldn't GC 
allocate.


Question about @nogc in D 2.066

2014-07-11 Thread Weasel via Digitalmars-d-learn

@nogc
void main(string[] args)
{
immutable(char)[] s1 = "hello";
immutable(int)[] a1 = [1, 2];
}

Why does the s1 not throw an error, but the a1 does? As far as I 
can tell, they're both immutable arrays.


Error is: "Error: array literal @nogc function main may cause GC 
allocation"


It compiles fine if I comment out the second line in main.


Re: Passing Templated Function Arguments Solely by Reference

2014-07-11 Thread Ali Çehreli via Digitalmars-d-learn

On 07/11/2014 03:38 AM, "Nordlöw" wrote:

> https://github.com/nordlow/justd/blob/master/random_ex.d
>
> is what I have so far. Does this look ok to you?

The following seems redundant because the other isFloatingPoint!E 
version uses the default arguments 0 and 1 anyway.


auto ref randInPlace(E)(ref E x) @trusted if (isFloatingPoint!E)
{
return x = uniform(cast(E)0,
   cast(E)1);
}

> Question: Can I somehow avoid the duplication of logic in
>
> - auto ref randInPlace(R)(R x) @safe if (hasAssignableElements!R)
> - auto ref randInPlace(T)(ref T x) @safe if (isStaticArray!T)

The following works if you pardon the name foo. :p

auto foo(TT)(ref TT x)
{
foreach (ref e; x)
{
e.randInPlace;
}
return x;
}

/** Generate Random Contents in $(D x).
 */
auto ref randInPlace(R)(auto ref R x) @safe if (hasAssignableElements!R)
{
return foo(x);
}

/** Generate Random Contents in $(D x).
 */
auto ref randInPlace(T)(ref T x) @safe if (isStaticArray!T)
{
return foo(x);
}

Alternatively, a string mixin could be used but they should be reserved 
for when there is no better solution.


Ali



Re: Value Reference Type Traits

2014-07-11 Thread anonymous via Digitalmars-d-learn

On Friday, 11 July 2014 at 16:10:31 UTC, Nordlöw wrote:

Is there a trait to check if a type is a

- value type (struct, static array, etc)
- reference type (class, dynamic array, string, etc)

?


There's http://dlang.org/phobos/std_traits.html#hasIndirections

Note that structs and static arrays may contain indirections,
i.e. they are not guaranteed to have value semantics all the way
down. Primitive types like int or float are definite values types.


Value Reference Type Traits

2014-07-11 Thread Nordlöw

Is there a trait to check if a type is a

- value type (struct, static array, etc)
- reference type (class, dynamic array, string, etc)

?


Re: core.exception.InvalidMemoryOperationError

2014-07-11 Thread francesco cattoglio via Digitalmars-d-learn

On Friday, 11 July 2014 at 11:43:44 UTC, Joakim wrote:
On Thursday, 10 July 2014 at 15:36:53 UTC, francesco cattoglio 
wrote:
A code I'm working on stops working and starts printing an 
infinite loop of

core.exception.InvalidMemoryOperationError
to the command line output. The code is quite complex and the 
bug seems to present itself almost in random situation so I 
would like to try to understand the issue better before 
looking for the wrong line of code hiding somewhere. I've read 
it might be that something is trying to allocate during a 
destructor call, but it sounds really strange to me that 
there's a neverending amount of exceptions being thrown. This 
is the first exception being thrown (nothing is thrown before 
the infinite loop begins).


Anyone has suggestions/ideas/heard of a similar stuff before?


If you look at the source for the garbage collector, the only 
place that error is called is if the gc is trying to malloc or 
execute other memory operations while the collector is running.
 I ran across this myself because an assert was getting 
triggered in a destructor.  Since an assert tries to malloc and 
the destructor is called by the GC, I got an 
InvalidMemoryOperationError which swallowed up the message from 
the original assert.


By putting printfs in the code path in druntime, I was able to 
track it down to that destructor, otherwise I had no idea where 
the invalid memory error was getting triggered.  You can 
probably do the same, but you can be sure it's a GC issue, and 
I would guess tied to allocating in a destructor (unless you 
happen to be calling InvalidMemoryOperationErrors somewhere in 
your own code or some library that you're using, which is 
unlikely).


It's unfortunate that you wrote this only 4 hours ago, because I 
already spent the morning doing more-or-less the same thing, and 
finaly realized what was happening and WHO was allocating during 
a destructor. :o) It's even somewhat told in the docs of 
core.exception module.
What I really don't understand is how the hell was it possible 
that something managed to either recurse or loop to generate an 
infinite WOE (Wall Of Exceptions).


DMDScript

2014-07-11 Thread Chris via Digitalmars-d-learn
Tried to compile on linux, got this error message (I guess I can 
fix it):


dmd -c textgen.d
textgen.d(36): Error: cannot implicitly convert expression 
("DMDScript fatal runtime error: ") of type string to char[]
textgen.d(36): Error: cannot implicitly convert expression (0) of 
type int to char[]
textgen.d(36): Error: cannot implicitly convert expression 
("ERR_RUNTIME_PREFIX") of type string to char[]
textgen.d(37): Error: cannot implicitly convert expression ("No 
default value for COM object") of type string to char[]
textgen.d(37): Error: cannot implicitly convert expression (0) of 
type int to char[]
textgen.d(37): Error: cannot implicitly convert expression 
("ERR_COM_NO_DEFAULT_VALUE") of type string to char[]
textgen.d(38): Error: cannot implicitly convert expression ("%s 
does not have a [[Construct]] property") of type string to char[]
textgen.d(38): Error: cannot implicitly convert expression (0) of 
type int to char[]
textgen.d(38): Error: cannot implicitly convert expression 
("ERR_COM_NO_CONSTRUCT_PROPERTY") of type string to char[]
textgen.d(39): Error: cannot implicitly convert expression 
("argument type mismatch for %s") of type string to char[]
textgen.d(39): Error: cannot implicitly convert expression (0) of 
type int to char[]
textgen.d(39): Error: cannot implicitly convert expression 
("ERR_DISP_E_TYPEMISMATCH") of type string to char[]
textgen.d(40): Error: cannot implicitly convert expression 
("wrong number of arguments for %s") of type string to char[]
textgen.d(40): Error: cannot implicitly convert expression (0) of 
type int to char[]
textgen.d(40): Error: cannot implicitly convert expression 
("ERR_DISP_E_BADPARAMCOUNT") of type string to char[]
textgen.d(41): Error: cannot implicitly convert expression ("%s 
Invoke() fails with COM error %x") of type string to char[]
textgen.d(41): Error: cannot implicitly convert expression (0) of 
type int to char[]
textgen.d(41): Error: cannot implicitly convert expression 
("ERR_COM_FUNCTION_ERROR") of type string to char[]
textgen.d(42): Error: cannot implicitly convert expression 
("Dcomobject: %s.%s fails with COM error %x") of type string to 
char[]
textgen.d(42): Error: cannot implicitly convert expression (0) of 
type int to char[]
textgen.d(42): Error: cannot implicitly convert expression 
("ERR_COM_OBJECT_ERROR") of type string to char[]

linux.mak:178: recipe for target 'textgen.o' failed
make: *** [textgen.o] Error 1


Re: get number of items in DList

2014-07-11 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jul 11, 2014 at 10:23:58AM -0300, Ary Borenszweig via 
Digitalmars-d-learn wrote:
> On 7/11/14, 4:46 AM, bearophile wrote:
> >pgtkda:
> >
> >>How can i get the number of items which are currently hold in a
> >>DList?
> >
> >Try (walkLength is from std.range):
> >
> >mydList[].walkLength
> >
> >Bye,
> >bearophile
> 
> So the doubly linked list doesn't know it's length? That seems a bit
> inefficient...

It should be relatively simple to write a wrapper that *does* keep track
of length.

The main problem, though, comes from list splicing: given two arbitrary
points in the list, if you splice out the section of the list in
between, there's no easy way to know how many items lie in between, so
you'll have to walk the list to recompute the length then. Which sorta
defeats the purpose of having a linked list. :)


T

-- 
Valentine's Day: an occasion for florists to reach into the wallets of nominal 
lovers in dire need of being reminded to profess their hypothetical love for 
their long-forgotten.


Re: get number of items in DList

2014-07-11 Thread bearophile via Digitalmars-d-learn

Ary Borenszweig:

So the doubly linked list doesn't know it's length? That seems 
a bit inefficient...


Have you tried to compile mydList.length or mydList[].length? If 
both don't compile, then you have to walk the items.


Walking the items is not efficient, but:
- Linked lists are very uncommonly needed. In 98-99% of cases an 
array of items or an array of pointers is better (faster for all 
operations, more efficient for memory used, leading to smaller 
binary, more compatible with other APIs, and so on).
- Most functional algorithms that work on lists do not need to 
know the length of the list.


Bye,
bearophile


Re: get number of items in DList

2014-07-11 Thread Ary Borenszweig via Digitalmars-d-learn

On 7/11/14, 4:46 AM, bearophile wrote:

pgtkda:


How can i get the number of items which are currently hold in a DList?


Try (walkLength is from std.range):

mydList[].walkLength

Bye,
bearophile


So the doubly linked list doesn't know it's length? That seems a bit 
inefficient...


Re: core.exception.InvalidMemoryOperationError

2014-07-11 Thread Joakim via Digitalmars-d-learn
On Thursday, 10 July 2014 at 15:36:53 UTC, francesco cattoglio 
wrote:
A code I'm working on stops working and starts printing an 
infinite loop of

core.exception.InvalidMemoryOperationError
to the command line output. The code is quite complex and the 
bug seems to present itself almost in random situation so I 
would like to try to understand the issue better before looking 
for the wrong line of code hiding somewhere. I've read it might 
be that something is trying to allocate during a destructor 
call, but it sounds really strange to me that there's a 
neverending amount of exceptions being thrown. This is the 
first exception being thrown (nothing is thrown before the 
infinite loop begins).


Anyone has suggestions/ideas/heard of a similar stuff before?


If you look at the source for the garbage collector, the only 
place that error is called is if the gc is trying to malloc or 
execute other memory operations while the collector is running.  
I ran across this myself because an assert was getting triggered 
in a destructor.  Since an assert tries to malloc and the 
destructor is called by the GC, I got an 
InvalidMemoryOperationError which swallowed up the message from 
the original assert.


By putting printfs in the code path in druntime, I was able to 
track it down to that destructor, otherwise I had no idea where 
the invalid memory error was getting triggered.  You can probably 
do the same, but you can be sure it's a GC issue, and I would 
guess tied to allocating in a destructor (unless you happen to be 
calling InvalidMemoryOperationErrors somewhere in your own code 
or some library that you're using, which is unlikely).


Re: Passing Templated Function Arguments Solely by Reference

2014-07-11 Thread Nordlöw

On Wednesday, 9 July 2014 at 07:43:57 UTC, Ali Çehreli wrote:

Ali


This

https://github.com/nordlow/justd/blob/master/random_ex.d

is what I have so far. Does this look ok to you?

Question: Can I somehow avoid the duplication of logic in

- auto ref randInPlace(R)(R x) @safe if (hasAssignableElements!R)
- auto ref randInPlace(T)(ref T x) @safe if (isStaticArray!T)

?


Re: How to interact with fortran code

2014-07-11 Thread bachmeier via Digitalmars-d-learn

On Thursday, 10 July 2014 at 12:12:20 UTC, Marc Schütz wrote:

On Wednesday, 9 July 2014 at 15:09:08 UTC, Chris wrote:

On Wednesday, 9 July 2014 at 15:00:25 UTC, seany wrote:
I apologize many times for this question, may be this had 
already been answered somewhere, but considering today the 
last of my nerve is broken, I can not really find the soution.


So I have a D code, which acts as a central manager of all my 
codes, reads user input, reads files, etc, and based on the 
file readouts, I would like to pass some variables from the D 
code to a fortran code, in binary format, perhaps, if such a 
thing exists, instead of encoding to text/ ASCII first.


I would also like to read some (not all) variables back from 
the fortran code.


The Fortran code resides in a subdirectory to the 
path/to/d/code


How to do this? is there a preffered way / easier than system 
call way to interface D and Fortran code? This must be Fortan 
code - these are the standard atmospheric chemistry codes.


I apologize again if the question is stupid, trust me, today 
all my nerves are broken.


Off the top of my head I'd say you could try to interface 
Fortran and C. Then you could interface D and C, i.e. D > C > 
Fortran.


http://fortranwiki.org/fortran/show/Generating+C+Interfaces
https://gcc.gnu.org/onlinedocs/gcc-3.4.4/g77/C-Interfacing-Tools.html


To expand on that:

You don't actually need to write a C glue layer between D and 
Fortran. All you need to do is make your Fortran functions 
accessible for C code. As I'm not familiar with Fortran, I 
don't know how exactly that works, but it could involve telling 
the compiler to use the right calling convention, and use the 
right name mangling.


If you are able to use Fortran 2003 or later, Fortran's 
iso_c_binding makes it simple to expose Fortran functions to be 
called from C and vice versa. I don't know of any good online 
references, but a lot of good examples can be found in

the source for the Fortran interface to the GSL:

http://www.lrz.de/services/software/mathematik/gsl/fortran/index.html

An example of compiling and linking:

http://compgroups.net/comp.lang.fortran/usage-of-iso_c_binding/155309

If iso_c_binding is not available, then it's not as pretty:

http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html


Re: Passing Templated Function Arguments Solely by Reference

2014-07-11 Thread Nordlöw

On Wednesday, 9 July 2014 at 07:43:57 UTC, Ali Çehreli wrote:
Phobos algorithms use ranges. The following is what I've come 
up with very quickly:


Thx


Re: get number of items in DList

2014-07-11 Thread bearophile via Digitalmars-d-learn

pgtkda:

How can i get the number of items which are currently hold in a 
DList?


Try (walkLength is from std.range):

mydList[].walkLength

Bye,
bearophile


get number of items in DList

2014-07-11 Thread pgtkda via Digitalmars-d-learn
How can i get the number of items which are currently hold in a 
DList?