Re: Use class template as a type

2016-11-30 Thread dm via Digitalmars-d-learn

On Tuesday, 29 November 2016 at 15:56:23 UTC, Jerry wrote:
To avoid having to use the Object class directly you can make 
an base class of the class template.

Like:

```
abstract class MyClass {}
abstract class MyClassImpl(T)
{
public:
@property const(T) value(){return _value;}
@property void value(T val){_value = val;}
 ...
   private:
T _value;
 ...
}

MyClassInt and float inherits from MyClassImpl
```

And use it like:

```
void main() {
   MyClass[] objs;
   objs ~= new MyClassFloat();
   objs ~= new MyClassInt();
}
```


Yes, but anyway you need to downcast if(MyClassBlahBlah subclass 
= cast(MyClassBlahBlah)obj)...

So it's not much sense to have base class or interface MyClass.


Re: Use class template as a type

2016-11-28 Thread dm via Digitalmars-d-learn

We have a handy dandy syntax for this:

if (MyClassInt subclass = cast(MyClassInt)value) {
writeln(subclass.value);
}

If it doesn't cast to said type (it will be null) that branch 
won't execute.


Hell yeah! It's works!
Thank you!


Re: Use class template as a type

2016-11-28 Thread dm via Digitalmars-d-learn
Thats because MyClass is a template class. Templates are note 
types, instansiations of  templates can be types.

e.g.

Myclass!float[] arr; // note this is not MyClass!(float[]);

will work. As Rikki suggested using Object[] instead will allow 
use to store classes of different types.


Maybe I must use some stub class or interface and override all 
methods...
But I so like D templates, as a result it's a small and easy to 
understand code.


Or actually it's maybe a XY problem. I'm trying to implement 
OpenGL material manager and for OpenGL uniforms I tried to write:

```
abstract class Uniform(T)
@property ...
@property ...
T _val;...
void method()...
...
class FloatUniform: Uniform!float
...
override void method()...

And in material class
class Material
...
Texture[] textures;
Uniform[] uniforms;
...
```
Maybe i'm totally wrong and better just use glUniformXXX... in my 
main app instead of

```
auto uniform = new SomeTypeUniform...
...
uniform.value = someValue;
```
?


Re: Use class template as a type

2016-11-28 Thread dm via Digitalmars-d-learn
On Monday, 28 November 2016 at 11:30:23 UTC, rikki cattermole 
wrote:
In your case I'd just swap out ``MyClass[] someArray;`` to 
``Object[] someArray;``.
But only because there are no members added without the extra 
typing in MyClass.


Remember types in meta-programming in D are not erased, they 
exist in the assembly and are unique. Unlike Java who did the 
implementation rather wrong.


I'm tried to use Object[], but got error
Error: no property 'value' for type 'object.Object'
I guess I must cast() to MyClassInt or MyClassFloat, but how can 
I do it?


Use class template as a type

2016-11-28 Thread dm via Digitalmars-d-learn

Hi.
Is it possible to write in D something like this?

```
abstract class MyClass(T)
{
  public:
   @property const(T) value(){return _value;}
   @property void value(T val){_value = val;}
...
  private:
   T _value;
...
}
...
class MyClassFloat: MyClass!float
...

class MyClassInt: MyClass!int
...

void main()
{
  MyClass[] someArray;
  someArray ~= new MyClassFloat();
...

  someArray ~= new MyClassInt();
...

  foreach(myClass; someArray)
   if(typeid(myClass) == typeid(MyClassInt))
myClass.value = 999;
   else
myClass.value = 123.45f;
...

}
```
When I trying to compile code like above I got
Error: class MyClass(T) is used as a type.



Re: How to kill whole application if child thread raises an exception?

2016-10-28 Thread dm via Digitalmars-d-learn
On Thursday, 27 October 2016 at 13:37:29 UTC, Steven 
Schveighoffer wrote:


Hm... what about:

import std.traits: Parameters;

auto mySpawn(F)(F func, Parameters!F params)
{
static auto callIt(F func, Parameters!F params)
{
try
{
return func(params);
}
catch(Throwable t)
{
// print the exception/error, e.g.:
import std.writeln;
writeln(e.toString());
// terminate program
import core.stdc.stdlib : exit;
exit(1);
}
}

return spawn(, func, params);
}

void main()
{
auto tID = mySpawn();
...
}

-Steve


I found the solution which works with dmd and ldc2:

...
import core.stdc.stdlib: _Exit;
_Exit(exitcode);
...



Re: How to kill whole application if child thread raises an exception?

2016-10-27 Thread dm via Digitalmars-d-learn

I found http://arsdnet.net/this-week-in-d/2016-aug-07.html
Maybe it's helps me.


Re: How to kill whole application if child thread raises an exception?

2016-10-27 Thread dm via Digitalmars-d-learn

On Friday, 28 October 2016 at 03:38:05 UTC, dm wrote:
On Thursday, 27 October 2016 at 13:37:29 UTC, Steven 
Schveighoffer wrote:

Hm... what about:
...


Main thread still running.


Actually it's depends on compiler.
With ldc2 main thread doesn't stop, but with dmd seems all ok:
root@proxytest:~# ./newthread
object.Exception@newthread.d(30): Everything is bad.

??:? void newthread.func() [0x451f52]
??:? void newthread.mySpawn!(void function()*).mySpawn(void 
function()*).callIt(void function()*) [0x452037]
??:? void std.concurrency._spawn!(void function(void 
function()*)*, void function()*)._spawn(bool, void function(void 
function()*)*, void function()*).exec() [0x4529f4]

??:? void core.thread.Thread.run() [0x46f1f1]
??:? thread_entryPoint [0x46ef1b]
??:? [0x42d00a3]
uncaught exception
dwarfeh(224) fatal error
Aborted



Re: How to kill whole application if child thread raises an exception?

2016-10-27 Thread dm via Digitalmars-d-learn
On Thursday, 27 October 2016 at 13:37:29 UTC, Steven 
Schveighoffer wrote:

Hm... what about:
...


Main thread still running.



How to kill whole application if child thread raises an exception?

2016-10-26 Thread dm via Digitalmars-d-learn

Thanks all.
I gues I must rewrote my app to send exeptions to other threads, 
use non blocking io, etc, etc.


Re: How to kill whole application if child thread raises an exception?

2016-10-26 Thread dm via Digitalmars-d-learn
On Wednesday, 26 October 2016 at 10:09:05 UTC, rikki cattermole 
wrote:

If you throw an error it should crash the entire application.
But really you need to set up sync points within your 
application to allow it to die gracefully.


I tried throw new Error... But main thread still working.
Tried with dmd v2.071.2 and ldc2 0.17.2. OS - Linux.


Re: How to kill whole application if child thread raises an exception?

2016-10-26 Thread dm via Digitalmars-d-learn
On Wednesday, 26 October 2016 at 09:43:10 UTC, rikki cattermole 
wrote:

```D
void entryPoint(alias func)() {
try {
func();
} catch (Exception e) {
import std.stdio;
writeln(e.toString());
}
}

void main() {
auto tid = spawn(!someFunc);
// ...

}
```


Well... This code shows me:
object.Exception@thread.d(6): I'm an exception


But my main thread still working :(
Why so strange default behavior do not kill other threads in case 
some of threads raise exception?

But thanks anyway.


Re: How to kill whole application if child thread raises an exception?

2016-10-26 Thread dm via Digitalmars-d-learn
On Wednesday, 26 October 2016 at 08:53:13 UTC, rikki cattermole 
wrote:

Simple, handle the exceptions on each thread.


I don't want handle exceptions. I want my application crash with 
exception description. Can you change my code above to show how 
it can be made?


How to kill whole application if child thread raises an exception?

2016-10-26 Thread dm via Digitalmars-d-learn

Hi. I tried code below:

import std.concurrency;
import std.stdio;

void func()
{
throw new Exception("I'm an exception");
}

void main()
{
auto tID = spawn();
foreach(line; stdin.byLine)
send(tID, "");
}

I expect my application will die immediatly, but main thread 
still running and I don't see any errors.

I want to kill all my threads if it is unhandled exception.
How can I do that?