Re: Using D in Android App

2016-04-18 Thread Dan Olson via Digitalmars-d-learn
Justice  writes:

> On Saturday, 16 April 2016 at 04:04:24 UTC, Justice wrote:
>> Is it difficult to create a D business like app and connect it to
>> android through java for the interface?
>>
>> I'd rather create all the complex stuff in D and either use it
>> natively through java(I need a UI).
>>
>> If it is workable, can the same be said for IOS(just recompile the D
>> source to the IOS architecture then use it in an IOS app for the
>> ui)?
>
> Anyone?

Short answer is it should work: see "Alpha support for mobile" links on
http://dlang.org/download.html

Joakim has been pushing Android support and I've been working on getting
iOS into the core language.  The caveat is that both are alpha support
and it is hard to predict when they will be first class D citizens.

Speaking for the iOS support, the downloadable compiler is based on
2.068 (LDC 0.17.0) and will move to whatever LDC supports after 1.0.0 is
released.  I don't know of any problems at this stage, as long as you
just want to side load your app.  Submitting to the App Store is another
matter.  It has not been done yet and Apple's requirement to include
embedded bitcode is missing currently.
-- 
Dan


Re: GC configuration docs

2016-01-05 Thread Dan Olson via Digitalmars-d-learn
Rainer Schuetze <r.sagita...@gmx.de> writes:

> On 05.01.2016 01:39, Dan Olson wrote:
>> I haven't played with any of the new GC configuration options introduced
>> in 2.067, but now need to.  An application on watchOS currently has
>> about 30 MB of RAM.  Is there any more documentation than the web page
>> https://dlang.org/spec/garbage.html or should I just browse druntime
>> code?
>
> I don't think there is more than that.
>
>>
>> Also pointers (pun maybe) on finding who has the references keeping
>> memory from
>> being collected.
>>
>
> There is a "leak detector" in the GC compiled into it the with
> -debug=LOGGING, but I guess noone has been using it the last couple of
> years (and it seems to me it doesn't really do anything more than
> printing allocations).
>
> I used -debug=PRINTF, -debug=PRINTF_COLLECT and -debug=PRINTF_TO_FILE
> in the past and grepped the resulting gcx.log. You might want to
> uncomment some printf in the mark function, too.

Thanks Raniner!


Re: Is this expected? default to public members in private class

2015-05-04 Thread Dan Olson via Digitalmars-d-learn
ketmar ket...@ketmar.no-ip.org writes:

 On Sun, 03 May 2015 18:07:20 -0700, Dan Olson wrote:

 It seems a private class or struct defaults to public members.  Just
 curious if this is intended.  I would have expected private all the way
 down unless overriden.

 i bet it is intended. protection of struct/class members is independed of 
 protection of struct/class itself. this is good for code consistency: no 
 changing of outer protection flags can alter struct/class inner 
 machinery. so if you changed your mind about protection level of some 
 global things, you shouldn't unnecessary fix your code in completely 
 unrelated places.

Thanks ketmar, that makes sense.  I am in midst of adding an option to
have dscanner skip private/package declarations when emitting etags (new
--etags option) and I want to match what the D lang spec would say is
private (if it did say).  For now I have to reverse engineer based on
compiler and forums, but don't want to mimic a compiler bug.


Is this expected? default to public members in private class

2015-05-03 Thread Dan Olson via Digitalmars-d-learn
It seems a private class or struct defaults to public members.  Just
curious if this is intended.  I would have expected private all the way
down unless overriden.

--- plugh.d
module plugh;
auto makeFoo() {return new Foo;}

private:

class Foo
{
void maybepriv() {}
private void priv() {}
public void pub() {}
}

--- xyzzy.d
module xyzzy;
import plugh;

void main()
{
auto f = makeFoo();
f.maybepriv();  // is accessible after all
//f.priv(); // err expected, member private
f.pub();
}

--
Dan Olson


Re: Weird OSX issue

2015-04-25 Thread Dan Olson via Digitalmars-d-learn
Jacob Carlborg d...@me.com writes:

 On 2015-04-24 20:37, Steven Schveighoffer wrote:

 So am I going crazy? Or is dmd doing things differently depending on
 where its environment is? Any compiler gurus out there understand why
 the symbol is different?

 I don't want to file a bug with this, because it seems dependent on
 installation location, would possibly not be reproducible.

 I can't reproduce this with DMD from DVM (compiler is installed in the
 user home directory).

I have lots of version laying around and they all worked fine on the
posted code.

But maybe a clue here...

$ ~/dmd2.066.0/osx/bin/dmd mod1.d
$ nm mod1.o | grep start
 U _D4core6thread6Thread5startMFZv
$ dmd mod1.d
$ nm mod1.o | grep start
 U _D4core6thread6Thread5startMFNbZC4core6thread6Thread

--- a/src/core/thread.d
+++ b/src/core/thread.d
@@ -587,7 +587,7 @@ class Thread
  * Throws:
  *  ThreadException if the thread fails to start.
  */
-final Thread start()
+final Thread start() nothrow
 in
 {
 assert( !next  !prev );

I wonder if dmd -v will show where its picking up stuff.


Re: std.json questions

2015-04-25 Thread Dan Olson via Digitalmars-d-learn
tired_eyes pastuho...@gmail.com writes:

 First issue: what is the proper (idiomatic) way to conver JSONValue
 to the proper types?

 Second: what is the proper way of handling boolean values in JSON (how
 to convert JSON_TYPE.TRUE and JSON_TYPE.FALSE to bool)?

 Righ now I'm doing is something like this:

 string data = readText(file.json);
 JSONValue[string] parsedData = parseJSON(data).object;
 JSONValue[] etities = stateData[entities].array;

 foreach(e; entities) {
 int x = to!int(e[x].integer);
 int y = to!int(e[y].integer);
 string texture = stripExtension(e[texture].str);

 auto isControllable = isControllable in e;

 if (isControllable !is null) {
 if (e[isControllable].type == JSON_TYPE.TRUE) {
 isControllable = true;
 } else {
 isControllable = false;
 }
 }
 }

 I think this is ugly and clunky approach, what is the beautiful one?

 A brief look at code.dlang.org gives us 7 (!) additional JSON
 libraries. Keeping in mind that D community isn't so huge, I think I'm
 not the only person struggling with std.json. Are there any plans on
 upgrading it?

Hi and welcome to D land.  I see discussions on how std.json needs to be
upgraded.  And it is not well documented.

I tried to progressively simplify the code that was posted to show what
can be done, but keeping the same spirit.  Not necessarily beautiful,
but less verbose code. I did not see a simpler way to deal with bools in
the std.json code.  Others here are experts on idiomatic D, they may
show something much better.

// First , make it work and show all types
void f1()
{
string data = readText(file.json);
JSONValue parsedData = parseJSON(data);
JSONValue entities = parsedData[entities];

foreach(size_t index, e; entities) {
long x = e[x].integer;
long y = e[y].integer;
string texture = stripExtension(e[texture].str);

bool isControllable = false;

if (isControllable in e) {
if (e[isControllable].type == JSON_TYPE.TRUE) {
isControllable = true;
} else {
isControllable = false;
}
}
writefln(x %d y %d texture %s isControllable %s,
 x, y, texture, isControllable);
}
}

// Next,  let compiler figure types for us
void f2()
{
auto data = readText(file.json);
auto parsedData = parseJSON(data);
auto entities = parsedData[entities];

foreach(size_t _, e; entities) {
auto x = e[x].integer;
auto y = e[y].integer;
auto texture = stripExtension(e[texture].str);

bool isControllable = false;

if (isControllable in e) {
isControllable = e[isControllable].type == JSON_TYPE.TRUE;
}
writefln(x %d y %d texture %s isControllable %s,
 x, y, texture, isControllable);
}
}

// A little simpler isControllable.
void f3()
{
auto parsedData = readText(file.json).parseJSON;

foreach(size_t _, e; parsedData[entities]) {
auto x = e[x].integer;
auto y = e[y].integer;
auto texture = stripExtension(e[texture].str);
auto isControllable = isControllable in e 
   e[isControllable].type == JSON_TYPE.TRUE;
writefln(x %d y %d texture %s isControllable %s,
 x, y, texture, isControllable);
}
}



Re: extern(C++) linker errors

2015-04-22 Thread Dan Olson via Digitalmars-d-learn
bitwise bitwise@gmail.com writes:
 I am trying to interface to C++, and getting linker errors. Below are
 my 3 source files and 2 build scripts with their associated
 errors. Can anyone see what I'm doing wrong?

Hi, I think both examples need libstdc++ added when you link
(-L-lstdc++).  That should resolve the missing C++ operators.

For script1.sh with ldc2 I see an extra underscore in C++ mangled names.
You must be on OS X.  ldc was just fixed in merge-2.067 branch to remove
an extra underscore on OS X.  If you want to experiment, you can build
https://github.com/ldc-developers/ldc/tree/merge-2.067 and check it out.
There are still a few tests to reolve, but it works pretty well for me.
--
Dan


Re: Are there any methods like isDestroyed or isDisposed to detect whether some object is destroyed or not?

2014-12-14 Thread Dan Olson via Digitalmars-d-learn
Marc Schütz\ schue...@gmx.net writes:

 On Saturday, 13 December 2014 at 21:20:43 UTC, Andrey Derzhavin wrote:

 import std.stdio;

 class ObjectAType {
bool ok;
this() {ok = true;}
 }

 void main()
 {
auto a = new ObjectAType;
assert(a.ok);
destroy(a);
assert(!a.ok);  // a has been destroyed.
 }


 This method of detection of collected objects is what I needed.
 Thanks to everybody for your advise.

 Be careful - the memory could still have been reused. For example:

 assert(a.ok);
 destroy(a);
 // ... lots of code, maybe multi-threaded ...
 assert(!a.ok);// can fail

 If between `destroy()` and the second `assert()`, the memory of the
 object is collected, a new object (of the same or different type) can
 have been placed there. You will then read an arbitrary piece of data
 from that new object, which may or may not evaluate to `true`.

In this case the object cannot be collected by gc because there is still
a good reference to it (variable 'a').  I agree if delete was used
instead of destroy that is would be unsafe.
--
dano


Re: mixin template had error by calling shared function

2014-12-11 Thread Dan Olson via Digitalmars-d-learn
Here is a way that will work.

Vlasov Roman vlasovroman...@yandex.ru writes:
 I have this code

 mixin template Template(void function() func1, void function() func2)

mixin template Template(alias func1, alias func2)

 class SomeClass {
   mixin Template!(func, func23);

mixin Template!(func, func23);

--
dano


Re: Tricky code with exceptions

2013-05-09 Thread Dan Olson
evilrat evilrat...@gmail.com writes:

 On Thursday, 9 May 2013 at 20:33:17 UTC, bearophile wrote:
 Brad Anderson:

 a 64-bit Windows dmd build did not crash and here is the output.

 Thank you for the data point. So maybe the problem is only on 32 bit
 Windows.

 Bye,
 bearophile

 win8 dmd 2.062 32-bit also crashes

Anybody looked at the assembly for the crashing executables?  The truth
is in there.

 


Re: Can D still compile html files? Seems not.

2013-02-26 Thread Dan Olson
Maybe you want Knuth's Literate Programming. 

http://en.wikipedia.org/wiki/Literate_programming

Long ago it was only for pascal and C (web and cweb), but now I see
there is noweb that works with any programming language.
-- 
Dan




Re: clear bug?

2011-09-05 Thread Dan Olson
Jonathan M Davis jmdavisp...@gmx.com writes:

 On Tuesday, September 06, 2011 02:12:11 Andrej Mitrovic wrote:
 On 9/6/11, Jonathan M Davis jmdavisp...@gmx.com wrote:
  Why would you
  ever try and use an object that had been cleared?
 
 TDPL, that's why. Things might have changed but how would someone new
 to D know that?

 I really don't pay much attention to clear, since I rarely use it, and I 
 don't 
 know why you'd ever actually want to try and use an object that you cleared. 
 I 
 do remember discussions about making it clear out the vtable, since you 
 _want_ 
 it to blow up after it's been cleared rather than have an invalid object work 
 on some level. Rereading the relevant section in TDPL though, it does look 
 like it describes a different situation than seems to have been ultimately 
 settled on. So, I guess that I don't know exactly what the situation 
 currently 
 is or what it's supposed to be. But I wouldn't have expected clear to result 
 in a valid object. But clearing out the vtable at least makes it blow up 
 nicely instead of doing who knows what with memory when you try and call 
 functions on an invalid object. So, if the ultimate goal is just to make it 
 memory safe, then both clearing out the vtable and the situation that TDPL 
 describes do the trick, though deleting the vtable actually helps tell you 
 that you've used an object when you shouldn't, whereas TDPL's description 
 results in using the object succeeding, which probably isn't a good thing.

 - Jonathan M Davis

Thanks for the clearification.

It was based on TDPL 6.3.5 that I thought the idea of clear() was to
clean up but leave the thing in an initialized state.  And because
clear() on dynamic arrays and other types seems to be be intended to
leave you with a valid and initialized object.  Or is that not true?

Dan


Debug on OSX?

2011-09-05 Thread Dan Olson
I tried dmd -gc like on linux but gdb on OSX doesn't seem happy.  Is
there a way to get the debuger to work with dmd on OSX?

Thanks,
Dan


clear bug?

2011-09-04 Thread Dan Olson
Using dmd 2.054 on osx:

Calling a instance method after using clear() on an instance is giving
me a bus error.  The instance fields look like they are cleared ok, just
can't safely call a method.  I think this used to work after the object
was cleared, and was the reason for clear() over the deprecated delete.

Dan
=-=-=
Sample:
import std.stdio;

class A
{
   string name = none;
   void print() {writeln(name);}
}

void main()
{
   A a = new A;
   a.print();   // none

   a.name = xyzzy;
   a.print();   // xyzzy

   clear(a);
   writeln(a.name); // a is reinit to none as expected
   a.print();   // bus error?
}

Output:
$ dmd clearbug.d
$ ./clearbug
none
xyzzy
none
Bus error


Re: const main args?

2011-08-20 Thread Dan Olson
mimocrocodil 4deni...@gmail.com writes:

 I seen what sendmail actually changes they arguments of command line for nice 
 output of ps ax command.

 May be it changes his argc/argv for this?

Yes.  Some unix C programs, daemons usually, modify argv to change what ps 
shows.  It works with D too, I tried it (but have to cast away immutable).   
Here is a quick demo, assuming args[0] has enough room.

import std.c.string;
import core.thread;

void main(string[] args)
{
   auto a = cast(char*)args[0].ptr;
   strcpy(a, xyzzy);  // change ps output to list us as xyzzy
   Thread.sleep(dur!(seconds)(10));
}

$ ps
  PID TTY   TIME CMD
  355 ttys0000:00.01 /bin/bash --noediting -i
  358 ttys0000:00.00 xyzzy

Since you have to be unsafe anyway, casting to char* from in string would be no 
worse than string.


Re: DMD Backend: Deciding instructions to use/avoid?

2011-06-05 Thread Dan Olson
Jacob Carlborg d...@me.com writes:

 I'm having an Intel Core 2 Duo. Don't know if it helps but when I run
 the pre-compiled DMD on the 6.06 box I get an errors message similar
 to: Floating point exception.

A long shot, but this sounds like a problem I hit trying to run some C
code binaries on an older system. Try googling for FPE and gnuhash. When
binaries are built with a newer ld option --hash-style=gnu, running on
systems with an older loader with fail wth an FPE. For backwards
compatability you have to use --hash-style=both or sysv.

I don't use D on linux and don't know how it passes options to ld. You
can use objdump to show if a .gnu.hash section is being used. And on
older systems, the man page for ld won't list --hash-style.

-- 
Dan


Re: How do I iterate over enum members at runtime?

2011-04-10 Thread Dan Olson
Jesse Phillips jessekphillip...@gmail.com writes:

 On Sat, 09 Apr 2011 16:20:06 -0400, Andrej Mitrovic wrote:

 I know there's traits to get strings at compile time, e.g.: auto b = [
 __traits(allMembers, Metrics) ];
 
 but this doesn't help me try out those enum values at runtime. It could
 almost work if I could use a mixin() in a foreach loop, but that's lucid
 dreaming.
 
 Another alternative might be to create an array out of the enum, but I
 don't know of any way of doing this.

 You have everything you need, just put them all together:

 enum Metrics : int
 {
   SM_CXSCREEN = 0,
   SM_CYSCREEN,
   SM_CXVSCROLL,
   SM_CYHSCROLL,
   SM_CYCAPTION,
   SM_CXBORDER,
 }

 void foo(int m)
 {
 }

 void main()
 {
 foreach (m; __traits(allMembers, Metrics))
 {
 foo(mixin(Metrics. ~ m));
 }
 }

I'm exploring more and found there is also std.traits.EnumMembers.  It's
a little simpler:

foreach (m; EnumMembers!Metrics))
{
foo(m);
}

And for number of members in enum Metrics

EnumMembers!Metrics.length

-- 
Dan


Re: Are function pointers compile time constants?

2011-02-26 Thread Dan Olson
d coder dlang.co...@gmail.com writes:

 Greetings

 I tried to initialize a struct member with a function pointer, and
 found that DMD2 did not like it. Are not function pointers compile
 time constants? And why they should not be?

 Regards
 - Cherry

I just want to point out that this *should* be doable in D.  At compile
time the function's address in the object code is a placeholder that the
linker or loader will fixup based on the function symbol.  This is what
linkers do!  And if an function ptr is used, the function can still be
inlined; just need to keep around the real function reference through
the pointer.

Dan


Re: Multiple assignment

2011-02-26 Thread Dan Olson
Jonathan M Davis jmdavisp...@gmx.com writes:

 On Friday, February 25, 2011 17:31:36 Ali Çehreli wrote:
 On 02/25/2011 05:09 PM, bearophile wrote:
int j;
int[2] y;
y[j] = j = 1;
 
 I think that's undefined behavior in C and C++. It is not defined
 whether j's previous or past value is used in y[j].
 
 I would expect the situation be the same in D.

 No, that should be perfectly defined. What's undefined is when you do
 something like func(j, y[j]). The evaluation order of the function
 arguments is undefined.  However, the evaluation order when dealing
 with an assignment should be defined.  I _could_ be wrong about that,
 but there's no question that the assignments themselves are guaranteed
 to be done in right-to-left order.

 - Jonathan M Davis

Java made assignment well defined by saying:
  First evaluate the left-hand-side to determine a variable to assign to.
  Then evaluate the right-hand-side for the value.

If the right-hand-side is another assignment, repeat...

So given:
int i = 0;
int[] a = new int[4];

a[i++] = a[i+=2] = i = 9;

You are can depend on getting:

i = 9
a = [9, 0, 0, 9]


D today on windows yields the same output.  Will the D language spec
make this the define behavior too?  I noticed that
http://www.digitalmars.com/d/2.0/expression.html currently says it is
implementation defined.  The example given is:

i = i++;

None of this is stuff you'd normally want to write unless entering an
obfuscated programming contest, but Java's rules say if i = 42, 'i' will end up
still being 42.

Dan


Re: Nested function declarations

2011-01-30 Thread Dan Olson
dennis luehring dl.so...@gmx.net writes:

 They're useful for testing:

 unittest {
  int foo();
  static assert (is(ReturnType!foo == int));
 }

 and else? is it worth?

Don't class function declarations have the same issue?  You can declare
but all you'll get is a link error.  Unless there some way like C++ to
provide a definition elsewhere.

class C
{
   int foo();
}

void main()
{
}

superarray_error.obj(superarray_error) 
 Error 42: Symbol Undefined _D16superarray_error1C3fooMFZi
--- errorlevel 1


Re: How to wrap SDL?

2011-01-23 Thread Dan Olson
Sean Eskapp eatingstap...@gmail.com writes:

 == Quote from Andrej Mitrovic (andrej.mitrov...@gmail.com)'s article
 You can use scoped!() from std.typecons:

snip

 You must not escape a reference to the object outside of the foo()
 scope. Note that you will get a runtime error if you try to do
 something like this:
 import std.stdio;
 import std.typecons;
 class A
 {
 ~this()
 {
 writeln(A destructor);
 }
 }
 auto foo()
 {
 auto a1 = scoped!A();
 return a1;
 }
 void main()
 {
 auto result = foo();
 writeln(exiting..);
 }
 Illegal call to Scoped this(this)
 A destructor
 core.exception.AssertError@std.typecons(2506): Assertion failure

 But these can't be passed to functions either, meaning I can't pass a Screen,
 Text, or Font wrapper around, all of which I use in my project!

It should be ok to pass A to a function for temporary use.

{
  auto a1 = scoped!A();
  dostuff(a1);
}

void dostuff(A a)
{
  // .. but don't hang on to 'a' because dtor will be eventually called
}

Dan


Re: Structs with pointers?

2011-01-23 Thread Dan Olson
bearophile bearophileh...@lycos.com writes:

 Is this another compiler bug?

 The situation is nice:

 struct Foo1 {}
 struct Foo2 { int x; }
 const struct Foo3 { int* p; }
 struct Foo4 { int* p; }
 void bar1(Foo1 f) {}
 void bar2(Foo2 f) {}
 void bar3(Foo3 f) {}
 void bar4(Foo4 f) {}
 void main() {
 const f1 = Foo1();
 bar1(f1); // no error
 const f2 = Foo2();
 bar2(f2); // no error
 const f3 = Foo3();
 bar3(f3); // no error
 const f4 = Foo4();
 bar4(f4); // error
 }

 Bye,
 bearophile

I was going to say its because structs are passed by value.  But then I
changed bar2 to bar2(ref Foo2 f) {f.x = 2;} and that compiled without
error and even changed f2.x to 2 when I printed it.  That seems like a
bug.



Re: Memory mapped IO

2011-01-10 Thread Dan Olson
Lars T. Kyllingstad pub...@kyllingen.nospamnet writes:

 On Sun, 09 Jan 2011 22:44:44 -0800, Dan Olson wrote:

 I'm exploring D for embedded work as a nice alternative to C/C++ for the
 32-bitters and am finding it has a nice set of features.  But, what is
 the best way handle memory mapped IO?  I don't see volatile like in C.
 Is writing asm {} the best way to ensure memory access?
 
 Thanks,
 Dan Olson

 Would std.mmfile be what you need?

 http://www.digitalmars.com/d/2.0/phobos/std_mmfile.html

 -Lars

Ok, thanks.  So I see that is a wrapper for mmap.  So that would be good
for user space code running on top of posix or win32.  But...

I'm more interested in the general embedded case with a small OS or no
OS (just ISRs and main loop).  I'm betting without volatile, asm {} is
the next best thing for tickling a controllers peripheral registers? Yes/No?

I searched the news groups and saw there used to be a volatile.  But it
looks like it was done away with because of how it was misused (like C
volatile) for thread sharing.  But this is different.  This is just
telling the compiler not to optimize away an access.

-- 
Dan Olson



Memory mapped IO

2011-01-09 Thread Dan Olson
I'm exploring D for embedded work as a nice alternative to C/C++ for the
32-bitters and am finding it has a nice set of features.  But, what is
the best way handle memory mapped IO?  I don't see volatile like in C.
Is writing asm {} the best way to ensure memory access?

Thanks,
Dan Olson