Re: Compile time vs run time -- what is the difference?

2022-12-31 Thread H. S. Teoh via Digitalmars-d-learn
On Sun, Jan 01, 2023 at 02:18:23AM +, Salih Dincer via Digitalmars-d-learn 
wrote:
> On Sunday, 1 January 2023 at 01:20:02 UTC, Ali Çehreli wrote:
> > On 12/31/22 16:42, H. S. Teoh wrote:
> > 
> > - runtime: The D runtime.
> 
> Sometimes I see runtime facilities used as compile time and I'm having
> a hard time distinguishing that. It is even possible to use it with
> meta-programming facilities.
[...]
> Do you have a solid recommendation about distinguishing?
[...]

https://wiki.dlang.org/Compile-time_vs._compile-time

;-)


T

-- 
MACINTOSH: Most Applications Crash, If Not, The Operating System Hangs


Re: Compile time vs run time -- what is the difference?

2022-12-31 Thread Salih Dincer via Digitalmars-d-learn

On Sunday, 1 January 2023 at 01:20:02 UTC, Ali Çehreli wrote:

On 12/31/22 16:42, H. S. Teoh wrote:

- runtime: The D runtime.


Sometimes I see runtime facilities used as compile time and I'm 
having a hard time distinguishing that. It is even possible to 
use it with meta-programming facilities.


It is a powerful feature that can help to improve the 
expressiveness and efficiency of D programs, but it can also make 
code more difficult to understand and maintain.


Do you have a solid recommendation about distinguishing?

SDB@79


Re: Hipreme's #7 Tip of the day - Using the out parameters

2022-12-31 Thread zjh via Digitalmars-d-learn

On Saturday, 31 December 2022 at 17:13:16 UTC, Hipreme wrote:

 [...]


Nice article.


Re: Compile time vs run time -- what is the difference?

2022-12-31 Thread Ali Çehreli via Digitalmars-d-learn

On 12/31/22 16:42, H. S. Teoh wrote:

> "runtime"

Going off topic, I've settled on three different spelling of that 
(those? :) ):


- run time: As in this topic, things can happen at run time.

- run-time: Adjective, as in run-time value of something.

- runtime: The D runtime.

Ali



Re: Address of a class object

2022-12-31 Thread Ali Çehreli via Digitalmars-d-learn

On 12/31/22 16:35, Paul wrote:

> Can I acquire the address of a class object,

Answering that question literally, yes, you can by casting the class 
variable to void*. But note: 'class object' means the instance of class 
in D.


> not a class variable (i.e.
> the instantiations of the class)

D terminology is different there: A class variable is a reference to the 
class object (that carries the member variables, etc. of a class instance.)


  auto c = new C();

'c' is the *variable*, providing access to the *object* in dynamic memory.

> but the object definition itself?

As H. S. Teoh answered, Python etc. can do that but not D's compilation 
model.


The following program tries to demonstrate that the members are offset 
two void* sizes further from the address of the object:


class C {
int i;
}

void main() {
auto c = new C();

const likelyOffsetOfMembers = 2 * (void*).sizeof;

// HERE:
const objectAddrInDynamicMemory = cast(void*)c;

assert(objectAddrInDynamicMemory + likelyOffsetOfMembers == &c.i);
}

If the class is defined as extern(C++), then you must replace 2 above 
with 1:


extern(C++) class C {
int i;
}

Ali



Re: Address of a class object

2022-12-31 Thread H. S. Teoh via Digitalmars-d-learn
On Sun, Jan 01, 2023 at 12:35:40AM +, Paul via Digitalmars-d-learn wrote:
> Hello.  Thanks for any assistance.
> 
> Can I acquire the address of a class object, not a class variable
> (i.e. the instantiations of the class) but the object definition
> itself?
> 
> ```d
> class MyClass {char c}
> ...
> MyClass MyClassVar;
> 
> writeln(&MyClassVar); // this compiles
> writeln(&MyClass);// this does not
> ```

What's your purpose in doing this?  Maybe if you explain what you're
trying to accomplish, we can better understand how to help you.

The class definition does not have an address, because it's an abstract
definition that only exists during compilation.  At runtime the class
definition isn't stored anywhere. So it isn't possible to take its
address.

There is, however, typeid(MyClass), which is a runtime structure that
gives you some amount of information about the class. But the use of
typeid is discouraged due to some inherent issues with its design.


T

-- 
Designer clothes: how to cover less by paying more.


Re: Compile time vs run time -- what is the difference?

2022-12-31 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Dec 28, 2022 at 02:31:45AM +, thebluepandabear via 
Digitalmars-d-learn wrote:
> I am reading through the free book on learning D by Ali Çehreli and I
> am having difficulties understanding the difference between compile
> time execution and run time execution in D language.

It's very simple: in a compiled language like D, your program goes
through 3 stages:

1) Source code: the human-readable text that you write.

2) Compilation: this is when the compiler is compiling your program.
   IOW, "compile time". Compile time happens once when you compile your
   program.

3) Binary executable: the result of compilation.  When you run this
   executable, that's "runtime".  Runtime happens every time you run the
   program.


Stage (2) is transient, and only happens inside the compiler. It can be
broken down into several steps:

a) Lexing & parsing: the compiler reads the source code, breaks it into
   tokens (keywords, identifiers, operators, etc.), and constructs from
   them an abstract syntax tree (AST) that represents your program. Here
   is where typos and syntax errors are detected.

b) Semantic analysis: the compiler processes the AST and assigns meaning
   to each corresponding programming construct. Here is where (some)
   logic errors are detected (reference to an undefined identifier,
   invalid operations on a data type, calling a function with the wrong
   number of arguments, etc.).

c) Code generation: based on the semantics the compiler assigned to your
   program, it emits a series of CPU instructions that implement these
   semantics.  These instructions are generally saved into either
   intermediate object files that must later be linked together, or
   directly into the final executable.


D's compile-time capabilities are mainly of two categories:

i) AST manipulation: templates, static if, static foreach, and
   pragma(msg) belong to this category. This generally happens between
   steps (a) and (b).

ii) CTFE (compile-time function evaluation): this happens somewhere
   around step (c), and mainly consists of the compiler interpreting
   part of the program using an internal interpreter in order to compute
   the value of a function at compile-time.  This is triggered when this
   value is required in order to resolve something that's needed during
   compilation.

For more details, see:

https://wiki.dlang.org/Compile-time_vs._compile-time


T

-- 
If you think you are too small to make a difference, try sleeping in a closed 
room with a mosquito. -- Jan van Steenbergen


Address of a class object

2022-12-31 Thread Paul via Digitalmars-d-learn

Hello.  Thanks for any assistance.

Can I acquire the address of a class object, not a class variable 
(i.e. the instantiations of the class) but the object definition 
itself?


```d
class MyClass {char c}
...
MyClass MyClassVar;

writeln(&MyClassVar); // this compiles
writeln(&MyClass);// this does not
```


Hipreme's #7 Tip of the day - Using the out parameters

2022-12-31 Thread Hipreme via Digitalmars-d-learn
So, after some time using D, I found out that `out` isn't used in 
so many cases, but when used, it can be quite confusing, because 
if you don't read the documentation, it will be unclear that 
something is an `out` parameter, specially if you're reading a 
code that is not yours. Before using `out`, I found myself using 
more the address operator (&), because it made clear that the 
target variable would be initialized, but there is an even better 
and safer way to do that:


So, the best practice when using `out` parameters that I found 
right now and it becomes way clearer and can increase your 
performance if you're in a hot path is by void initializing your 
out variable, e.g:


```d

void initializeFloat(out float f){f = 0.0f;}
void main()
{
float myFloat = void;
initializeFloat(myFloat);
}

```

See that without the `void` initialization, all that rests is the 
function name which is quite readable `initializeFloat` so, 
something should happen to it. But there are cases which your 
naming won't be enough expressive to say that you're using an 
`out` parameter. This situation, void initialization (no 
initialization at all), is both faster and self documenting for 
your code, so, try to remember that feature, it took me some time 
to start using and I just remembered it existed when I was 
dealing with returning float vertices and there was so many that 
it appeared on my profiler.