April 15, 2021
On Thu, Apr 15, 2021 at 08:56:18PM +0000, 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

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

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:

mixin("class MyDynamicClassName { }");
MyDynamicClassName cls = new MyDynamicClassName;

Yes but not at runtime

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

April 15, 2021
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

April 16, 2021

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...
April 16, 2021

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:

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

April 16, 2021

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.

April 16, 2021

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

1 2
Next ›   Last »