Re: How do I create classes dynamically?

2021-04-16 Thread Imperatorn via Digitalmars-d-learn

On Friday, 16 April 2021 at 18:42:35 UTC, Kagamin wrote:

On Friday, 16 April 2021 at 08:31:27 UTC, Imperatorn wrote:

One example would be a repl


That has little to do with what OP meant.


I didn't answer to that though


Re: How do I create classes dynamically?

2021-04-16 Thread Kagamin via Digitalmars-d-learn

On Friday, 16 April 2021 at 08:31:27 UTC, Imperatorn wrote:

One example would be a repl


That has little to do with what OP meant.


Re: How do I create classes dynamically?

2021-04-16 Thread Imperatorn via Digitalmars-d-learn

On Thursday, 15 April 2021 at 22:06:23 UTC, Jack wrote:

On Thursday, 15 April 2021 at 17:48:02 UTC, Imperatorn wrote:

On Thursday, 15 April 2021 at 16:39:30 UTC, Kagamin wrote:

On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:

[...]


String mixins is D replacement of macros for code generation.
Works like this:
```d
mixin("class MyDynamicClassName { }");
MyDynamicClassName cls = new MyDynamicClassName;
```


Yes but not at runtime


Could you give an example of what you're trying to archive?


One example would be a repl


Re: How do I create classes dynamically?

2021-04-16 Thread novice3 via Digitalmars-d-learn

On Thursday, 15 April 2021 at 20:56:18 UTC, mw wrote:


In response to user input?

e.g. createObject(userInputString);



what if user enter "Cthulhu"?

if you say "i will check user input",
then you know list of possible inputs,
then you can use some compile-time technics.

if you dont know possible user inputs,
and user can input anything,
then questions raised:
- what is object Cthulhu meaning?
- what properties and methods it has?
etc...


Re: How do I create classes dynamically?

2021-04-15 Thread Ali Çehreli via Digitalmars-d-learn

On 4/15/21 1:56 PM, mw wrote:

>>> I wanted to find out if it is possible to create classes dynamically.
>>
>> out of curiosity: Why you would like to do this? I cannot think of a
>> use case for this - this is why i ask.
>
> In response to user input?

That's a different question because creating a class is different from 
creating instances (objects) of existing classes.


> again, one can manually dispatch, but is there a way to avoid this
> tediousness?

H. S. Teoh showed methods where lookup keys are known at compile time 
and are used to generate e.g. a switch statement.


I have a system where the main logic of the program has no idea what 
types are out there. All it has is a lookup table. (Uncompiled pseude 
code follows.)


struct Functions {
  void * function deserialize(ubyte[] bytes);
  void function process(void* input, void* output);
  void function serialize(ubyte[] bytes);
}

shared Functions[string] registration;

So, the main logic uses user input to look up what the functions are:

  auto funcs = registration[userInput];

And applies those functions to some data at runtime.

This is "dynamic" because registration is populated by the 'shared 
static this()' blocks of unknown modules that are loaded dynamically 
(and in my case conditionally):


module foo;

// This struct is what this module is about:
struct Foo {
  // ...
}

// The module registers itself with the main lookup system when loaded:
shared static this() {
  register("my lookup string",
   Functions(&deserializer!Foo,
 &processor!Foo,
 &serializer!Foo));
}

Note that registered functions are template instances tailored for this 
specific type. However, the main logic has no klowledge of individual 
modules, not even the templates like 'deserializer': It only knows about 
a lookup table that contains some function pointers.


I haven't used object creation functions above but that can be added to 
Functions as well.


Ali



Re: How do I create classes dynamically?

2021-04-15 Thread Jack via Digitalmars-d-learn

On Thursday, 15 April 2021 at 17:48:02 UTC, Imperatorn wrote:

On Thursday, 15 April 2021 at 16:39:30 UTC, Kagamin wrote:

On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:

[...]


String mixins is D replacement of macros for code generation.
Works like this:
```d
mixin("class MyDynamicClassName { }");
MyDynamicClassName cls = new MyDynamicClassName;
```


Yes but not at runtime


Could you give an example of what you're trying to archive?


Re: How do I create classes dynamically?

2021-04-15 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Apr 15, 2021 at 08:56:18PM +, mw via Digitalmars-d-learn wrote:
[...]
> of course, one can manually dispatch:
> 
> if  (userInputString == "cat") createCat();
> else if (userInputString == "dog") createDog();
> ...
> 
> but this this tedious.

---
// Disclaimer: this is proof of concept, I didn't actually run this yet
class Animal {}
class Cat : Animal {}
class Dog : Animal {}

alias SupportedTypes = AliasSeq!(Cat, Dog, /* whatever else you want here */);

string userInputString = ...;
Animal result;
SW: switch (userInputString) {
static foreach (T; SupportedTypes) {
case T.stringof:
result = new T;
break SW;
}
default:
throw new Exception("Unknown object type");
}
---


> I have a similar question: how to dynamically use user's input string
> as function name can call it? suppose the function has no argument.

Same idea:

---
// Disclaimer: this is proof of concept, I didn't actually run this yet
struct Dispatcher {
void bark() { ... }
void meow() { ... }
void moo() { ... }
... // whatever else you want here
}

Dispatcher disp;
string userInputString = ...;
SW: switch (userInputString) {
static foreach (fieldName; __traits(allMembers, disp)) {
static if (is(typeof(__traits(getMember, disp, fieldName))
== function)
{
case fieldName:
__traits(getMember, disp, fieldName)();
break SW;
}
}
}
---

Basically, the idea is to obtain a list of types/methods/whatever
somehow (either by explicitly listing instances, or via compile-time
introspection), then statically generate switch cases from it.  You can
eliminate many kinds of boilerplate using this little trick.


T

-- 
Береги платье снову, а здоровье смолоду. 


Re: How do I create classes dynamically?

2021-04-15 Thread mw via Digitalmars-d-learn

On Thursday, 15 April 2021 at 18:21:16 UTC, Martin wrote:

On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:
I wanted to find out if it is possible to create classes 
dynamically.


out of curiosity: Why you would like to do this? I cannot think 
of a use case for this - this is why i ask.


In response to user input?

e.g. createObject(userInputString);

of course, one can manually dispatch:

if  (userInputString == "cat") createCat();
else if (userInputString == "dog") createDog();
...

but this this tedious.


I have a similar question: how to dynamically use user's input 
string as function name can call it? suppose the function has no 
argument.


callFunc(userInputString)();  // call that func.

again, one can manually dispatch, but is there a way to avoid 
this tediousness?






Re: How do I create classes dynamically?

2021-04-15 Thread Martin via Digitalmars-d-learn

On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:
I wanted to find out if it is possible to create classes 
dynamically.


out of curiosity: Why you would like to do this? I cannot think 
of a use case for this - this is why i ask.


Re: How do I create classes dynamically?

2021-04-15 Thread Imperatorn via Digitalmars-d-learn

On Thursday, 15 April 2021 at 16:39:30 UTC, Kagamin wrote:

On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:
Maybe I am just too short in D, but I wanted to find out if it 
is possible to create classes dynamically. My problem is, I 
just don't know where to start reading. Maybe at mixin 
templates?


CreateClassWithName!("MyDynamicClassName");

should create the following class to work with dynamically:

class MyDynamicClassName {
this() { writeln("I was not written, but still I exist!"); 
}

}

So that I in the end by means of

MyDynamicClassName cls = new MyDynamicClassName;


String mixins is D replacement of macros for code generation.
Works like this:
```d
mixin("class MyDynamicClassName { }");
MyDynamicClassName cls = new MyDynamicClassName;
```


Yes but not at runtime


Re: How do I create classes dynamically?

2021-04-15 Thread Kagamin via Digitalmars-d-learn

On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:
Maybe I am just too short in D, but I wanted to find out if it 
is possible to create classes dynamically. My problem is, I 
just don't know where to start reading. Maybe at mixin 
templates?


CreateClassWithName!("MyDynamicClassName");

should create the following class to work with dynamically:

class MyDynamicClassName {
this() { writeln("I was not written, but still I exist!"); }
}

So that I in the end by means of

MyDynamicClassName cls = new MyDynamicClassName;


String mixins is D replacement of macros for code generation.
Works like this:
```d
mixin("class MyDynamicClassName { }");
MyDynamicClassName cls = new MyDynamicClassName;
```


Re: How do I create classes dynamically?

2021-04-15 Thread Imperatorn via Digitalmars-d-learn

On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:
Maybe I am just too short in D, but I wanted to find out if it 
is possible to create classes dynamically. My problem is, I 
just don't know where to start reading. Maybe at mixin 
templates?


CreateClassWithName!("MyDynamicClassName");

should create the following class to work with dynamically:

class MyDynamicClassName {
this() { writeln("I was not written, but still I exist!"); }
}

So that I in the end by means of

MyDynamicClassName cls = new MyDynamicClassName;

can work with it.

Normally I would think of a macro first, but as far as I 
understood D doesn't know macros (which I'm not really sad 
about), but maybe I'm just thinking too complicated yet. I 
would appreciate any hints, because as I said, I don't even 
know where to start reading.


ldc.JIT presumably has smth like that

https://gist.github.com/eldar/2294388

Also look up ldc.dynamic_compile


Re: How do I create classes dynamically?

2021-04-15 Thread Ali Çehreli via Digitalmars-d-learn

On 4/14/21 1:38 PM, Mario wrote:

> Maybe I am just too short in D, but I wanted to find out if it is
> possible to create classes dynamically.

In D world, "dynamically" means "at run time".

> Maybe at mixin templates?

Both mixins and templates are compile time features.

> Normally I would think of a macro first

If we are talking about C (and C++) macros, they are compile time 
features as well.


So, I think you want help from D to generate types, which can happen 
only at compile time with statically-typed languages like D. And yes, D 
is a great language for "generative programming" like that.


> "I was not written, but still I exist!"

Not possible by this "programming language", which wants a source code 
written, by a human or a machine, to be compiled. Still, anything is 
possible: For example, you can generate source code, dispatch a 
compiler, and load dynamically at run time. :)


Ali



Re: How do I create classes dynamically?

2021-04-14 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:
Maybe I am just too short in D, but I wanted to find out if it 
is possible to create classes dynamically. My problem is, I 
just don't know where to start reading. Maybe at mixin 
templates?


What exactly do you mean?

Your goal is probably achievable through some means, like I can 
kinda make D classes with a scripting language even.


Re: How do I create classes dynamically?

2021-04-14 Thread Mario via Digitalmars-d-learn
On Wednesday, 14 April 2021 at 20:47:57 UTC, Steven Schveighoffer 
wrote:
There is no good supported way to do this with the current 
runtime.


The way this is normally done is to use your own reflection 
system, and build a mechanism to create classes based on their 
name.


There was an old way, using Object.factory, but I would not 
recommend that, it's brittle and doesn't always work.


-Steve


Too bad. Nevertheless, thank you for your quick answer to my 
question.


-Mario


Re: How do I create classes dynamically?

2021-04-14 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/14/21 4:38 PM, Mario wrote:
Maybe I am just too short in D, but I wanted to find out if it is 
possible to create classes dynamically. My problem is, I just don't know 
where to start reading. Maybe at mixin templates?


CreateClassWithName!("MyDynamicClassName");

should create the following class to work with dynamically:

class MyDynamicClassName {
    this() { writeln("I was not written, but still I exist!"); }
}

So that I in the end by means of

MyDynamicClassName cls = new MyDynamicClassName;

can work with it.

Normally I would think of a macro first, but as far as I understood D 
doesn't know macros (which I'm not really sad about), but maybe I'm just 
thinking too complicated yet. I would appreciate any hints, because as I 
said, I don't even know where to start reading.




There is no good supported way to do this with the current runtime.

The way this is normally done is to use your own reflection system, and 
build a mechanism to create classes based on their name.


There was an old way, using Object.factory, but I would not recommend 
that, it's brittle and doesn't always work.


-Steve


How do I create classes dynamically?

2021-04-14 Thread Mario via Digitalmars-d-learn
Maybe I am just too short in D, but I wanted to find out if it is 
possible to create classes dynamically. My problem is, I just 
don't know where to start reading. Maybe at mixin templates?


CreateClassWithName!("MyDynamicClassName");

should create the following class to work with dynamically:

class MyDynamicClassName {
this() { writeln("I was not written, but still I exist!"); }
}

So that I in the end by means of

MyDynamicClassName cls = new MyDynamicClassName;

can work with it.

Normally I would think of a macro first, but as far as I 
understood D doesn't know macros (which I'm not really sad 
about), but maybe I'm just thinking too complicated yet. I would 
appreciate any hints, because as I said, I don't even know where 
to start reading.