Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 14, 2013 runtime evaluation | ||||
---|---|---|---|---|
| ||||
If I read it correctly, there is no possibility to create classes, structs, templates, functions or to import libs at runtime. Is there already an approach to add some functionality? I know, that D is a compiled language, but Java is too and supports class loading at runtime or more in general: runtime evaluation. |
December 14, 2013 Re: runtime evaluation | ||||
---|---|---|---|---|
| ||||
Posted in reply to hoya | hoya: > If I read it correctly, there is no possibility to create classes, structs, templates, functions or to import libs at runtime. Right. > Is there already an approach to add some functionality? > I know, that D is a compiled language, but Java is too and supports class loading at runtime or more in general: runtime evaluation. Java often runs on a JVM, so it keeps its compiler around at run-time, unlike most cases in D. With enough work you can do the same in D with the LLVM, and keep the compiler as a service at run-time, if you want even as a JIT. But currently ldc2 doesn't have this feature. Currently probably you can synthesize D source code, save the source, call the D compiler from D, and use it as library. Bye, bearophile |
December 14, 2013 Re: runtime evaluation | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Saturday, 14 December 2013 at 13:04:47 UTC, bearophile wrote:
> Currently probably you can synthesize D source code, save the source, call the D compiler from D, and use it as library.
>
> Bye,
> bearophile
Thanks, bearophile. But how to do this, if it is not possible to import a library at runtime?
|
December 14, 2013 Re: runtime evaluation | ||||
---|---|---|---|---|
| ||||
Posted in reply to hoya | Am Sat, 14 Dec 2013 13:15:13 +0100 schrieb "hoya" <sighoya@gmail.com>: > If I read it correctly, there is no possibility to create classes, structs, templates, functions or to import libs at runtime. You can load libraries at runtime like OpenGL or a database driver. This applies to all C and D libraries as well as C++ libraries to some extent. > Is there already an approach to add some functionality? > I know, that D is a compiled language, but Java is too and > supports class loading at runtime or more in general: runtime > evaluation. Did something change in recent Java versions? E.g. to get a create a new class I needed the JDK around (not just the JRE) for the compiler, which had to be added to the classpath first. I wouldn't have considered that a 1st class language feature. After all you compile your source code first with the optional development tools and then load the resulting class file which is not much different from calling the D compiler on some D source to create a library and loading that. Or does Java have an eval() method of some sort now? -- Marco |
December 14, 2013 Re: runtime evaluation | ||||
---|---|---|---|---|
| ||||
Posted in reply to hoya | On 2013-12-14 13:15, hoya wrote: > If I read it correctly, there is no possibility to create classes, > structs, templates, functions or to import libs at runtime. > Is there already an approach to add some functionality? > I know, that D is a compiled language, but Java is too and supports > class loading at runtime or more in general: runtime evaluation. You can instantiate classes at runtime using Object.factory. You can also loaded classes using dynamic libraries, just as in C or C++. -- /Jacob Carlborg |
December 14, 2013 Re: runtime evaluation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marco Leise | On Saturday, 14 December 2013 at 13:44:01 UTC, Marco Leise wrote: > You can load libraries at runtime like OpenGL or a database > driver. This applies to all C and D libraries as well as C++ > libraries to some extent. > Can you give me an example how to do this? > Or does Java have an eval() method of some sort now? Not directly, you can read this: http://rosettacode.org/wiki/Runtime_evaluation/In_an_environment#Java |
December 14, 2013 Re: runtime evaluation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Saturday, 14 December 2013 at 13:45:00 UTC, Jacob Carlborg wrote:
> You can instantiate classes at runtime using Object.factory. You can also loaded classes using dynamic libraries, just as in C or C++.
Yes, but loading a dynamic lib (shared) in C/C++ is at compile type, if I remembering right. But is there a way in D like dlopen() in C, which can be used at runtime?
|
December 14, 2013 Re: runtime evaluation | ||||
---|---|---|---|---|
| ||||
Posted in reply to hoya | On 12/14/2013 11:07 PM, hoya wrote: > On Saturday, 14 December 2013 at 13:45:00 UTC, Jacob Carlborg wrote: >> You can instantiate classes at runtime using Object.factory. You can >> also loaded classes using dynamic libraries, just as in C or C++. > Yes, but loading a dynamic lib (shared) in C/C++ is at compile type, if > I remembering right. But is there a way in D like dlopen() in C, which > can be used at runtime? > That's what Jacob is talking about. D can interface with C libraries just fine, so dlopen() is available on Posix systems and LoadLibrary on Windows. That's the approach I use with the C library bindings in Derelict[1]. Shared libraries are never loaded at compile time. You can link with an import library (on Windows) or directly with the shared object, but that just sets the executable up so that the library is loaded by the OS when the app starts up. dlopen and friends eliminate the need for a link step, but require more effort to load manually. [1] https://github.com/DerelictOrg |
December 14, 2013 Re: runtime evaluation | ||||
---|---|---|---|---|
| ||||
Posted in reply to hoya | Am Sat, 14 Dec 2013 14:59:42 +0100 schrieb "hoya" <sighoya@gmail.com>: > On Saturday, 14 December 2013 at 13:44:01 UTC, Marco Leise wrote: > > > You can load libraries at runtime like OpenGL or a database driver. This applies to all C and D libraries as well as C++ libraries to some extent. > > > Can you give me an example how to do this? Hmm, there are two options when it comes to dynamic linking. You can directly refer to the functions you use from some library. An example is std.net.curl in Phobos. You compile your program with "dmd -L-lcurl <source>" to link to the curl library which is written in C and common on Linux systems. This is the simple case where you don't need to write any extra code. If you want to load a plugin at runtime or OpenGL (which is a different library for each graphics card vendor) you will need to use the dynamic linker (libdl.so) which handles this case: "dmd -L-ldl <source>" and then use "dlopen" and "dlsym" in your program to find the addresses of functions. Derelict 3 is a good example of loading libraries at runtime this way: https://github.com/aldacron/Derelict3 It is a wrapper/binding collection for many multimedia/game related libraries and you can reuse its derelict.util module to load your own libraries at runtime. Which case are you interested in? > > Or does Java have an eval() method of some sort now? > Not directly, you can read this: http://rosettacode.org/wiki/Runtime_evaluation/In_an_environment#Java But that is what I described above. A Java compiler is not a requirement for a proper Java installation. It is an external tool that they load and the expression evaluation is done by writing the code to a Java source file on the file system, compiling that and loading it. This can be done with dmd as well, but in both cases it is crutch to bring eval()-like functionality to a statically compiled language. :) -- Marco |
December 14, 2013 Re: runtime evaluation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marco Leise | On Saturday, 14 December 2013 at 17:47:34 UTC, Marco Leise wrote: > Am Sat, 14 Dec 2013 14:59:42 +0100 > schrieb "hoya" <sighoya@gmail.com>: > >> On Saturday, 14 December 2013 at 13:44:01 UTC, Marco Leise wrote: >> >> > You can load libraries at runtime like OpenGL or a database >> > driver. This applies to all C and D libraries as well as C++ >> > libraries to some extent. >> > >> Can you give me an example how to do this? > > Hmm, there are two options when it comes to dynamic linking. > > You can directly refer to the functions you use from some > library. An example is std.net.curl in Phobos. You compile > your program with "dmd -L-lcurl <source>" to link to the curl > library which is written in C and common on Linux systems. > This is the simple case where you don't need to write any > extra code. > > If you want to load a plugin at runtime or OpenGL (which is a > different library for each graphics card vendor) you will need > to use the dynamic linker (libdl.so) which handles this case: > "dmd -L-ldl <source>" and then use "dlopen" and "dlsym" in > your program to find the addresses of functions. Derelict 3 is > a good example of loading libraries at runtime this way: > https://github.com/aldacron/Derelict3 > It is a wrapper/binding collection for many multimedia/game > related libraries and you can reuse its derelict.util module > to load your own libraries at runtime. > > Which case are you interested in? > The Latter > This can be done with dmd as > well, but in both cases it is crutch to bring eval()-like > functionality to a statically compiled language. :) Hmm. An eval() method is good, if writing something like a Drepl. I've heard from someone that other people have tried some work on it. Or good case, if you can use a dynamic mixin(). When read a string from input, then it is possible to create a structure like struct, class and so on. With dlopen() you need at least a library file, where all your code is written in, as I thin correctly. But with a dynamic mixin() you have the ability, to add structures in-memory. So this is more efficient. |
Copyright © 1999-2021 by the D Language Foundation