Re: How to properly use variadic templates (functions)?

2021-12-21 Thread rempas via Digitalmars-d-learn
On Tuesday, 21 December 2021 at 17:33:09 UTC, Steven Schveighoffer wrote: The reason your original isn't working is that indexing a list of differently-typed things cannot be done using a runtime index. I'd say that an inner function + static foreach + switch is the best way to convert from

Re: FreeBSD 13 : wrong kernel version and size of kevent_t

2021-12-21 Thread rempas via Digitalmars-d-learn
I would love to see more full (and correct) support for FreeBSD, OpenBSD and DragonflyBSD from Dlang! Maybe one day..

Re: FreeBSD 13 : wrong kernel version and size of kevent_t

2021-12-21 Thread rempas via Digitalmars-d-learn
On Tuesday, 21 December 2021 at 18:35:43 UTC, eugene wrote: LDC2 stdlib does not have correct struct event_t for 12+ in /usr/local/include/d/core/sys/freebsd/sys/event.d at all, only for earlier versions, ie without extension field. DMD stdlib does have, the only problem is the OS version is w

Re: FreeBSD 13 : wrong kernel version and size of kevent_t

2021-12-21 Thread rempas via Digitalmars-d-learn
On Tuesday, 21 December 2021 at 19:00:04 UTC, eugene wrote: Well, I just installed it by pkg intstall ldc Actually, this is my first experience with FreeBSD, I did not have much to go deeper. That's nice trying new things ;) Oh, no, sorry. :) It is the problem of FreeBSD maintainers to

Re: FreeBSD 13 : wrong kernel version and size of kevent_t

2021-12-21 Thread rempas via Digitalmars-d-learn
On Tuesday, 21 December 2021 at 19:40:52 UTC, eugene wrote: But i like to use software out of the box and do not like compile it from source. Me too! The link I gave you has a binary release with LDC2. You won't have to compile it yourself. I do not see any reason for me to do this. The "p

Re: FreeBSD 13 : wrong kernel version and size of kevent_t

2021-12-21 Thread rempas via Digitalmars-d-learn
On Tuesday, 21 December 2021 at 19:55:13 UTC, eugene wrote: core/sys/freebsd/config.d and core/sys/freebsd/sys/event.d are the same as in fresh dmd, so there is not much sense to try it. dmd (including phobos) is a reference, and I do not think, that ldc is ahead of dmd with it's stdlib. Oh,

Re: FreeBSD 13 : wrong kernel version and size of kevent_t

2021-12-21 Thread rempas via Digitalmars-d-learn
On Tuesday, 21 December 2021 at 21:09:14 UTC, eugene wrote: * The ldc installed by 'pkg install ldc' (the old one), does not have config module * Most resent ldc (link you indicated), does have condig module, and it is exactly the same as condig in most recent dmd Cool! Makes sense! I though

Re: How to properly use variadic templates (functions)?

2021-12-22 Thread rempas via Digitalmars-d-learn
On Tuesday, 21 December 2021 at 22:50:57 UTC, russhy wrote: I took a look and to be honest, it's the same story as everything in the STD, they try to do everything at the same time, so they up end calling each other, you end up lost in multiple 8k LOC modules, not understanding what the funct

Re: FreeBSD 13 : wrong kernel version and size of kevent_t

2021-12-22 Thread rempas via Digitalmars-d-learn
On Wednesday, 22 December 2021 at 08:41:56 UTC, eugene wrote: You thought right - I **DID NOT** try the latest LDC, I just looked into ldc2-1.28.0-freebsd-x86_64.tar.xz archive to see what is in core/sys/freebsd/config.d and in core/sys/freebsd/sys/event.d Both these files are just copies fro

Re: FreeBSD 13 : wrong kernel version and size of kevent_t

2021-12-22 Thread rempas via Digitalmars-d-learn
On Wednesday, 22 December 2021 at 09:49:59 UTC, eugene wrote: It looks strange - leading D compiler is not in Linux/FreeBSD repos :) Well no so much. The only official compiler is DMD and when it comes to that, Digital Mars, has pre-built binaries for most Linux distros and for FreeBSD. Now

How to print unicode characters (no library)?

2021-12-26 Thread rempas via Digitalmars-d-learn
Hi! I'm trying to print some Unicode characters using UTF-8 (char), UTF-16 (wchar) and UTF-32 (dchar). I want to do this without using any library by using the "write" system call directly with 64-bit Linux. Only the UTF-8 solution seems to be working as expected. The other solutions will not p

Re: How to print unicode characters (no library)?

2021-12-26 Thread rempas via Digitalmars-d-learn
On Sunday, 26 December 2021 at 21:22:42 UTC, Adam Ruppe wrote: write just transfers a sequence of bytes. It doesn't know nor care what they represent - that's for the receiving end to figure out. Oh, so it was as I expected :P You are mistaken. There's several exceptions, utf-16 can come in

Re: How to print unicode characters (no library)?

2021-12-26 Thread rempas via Digitalmars-d-learn
On Sunday, 26 December 2021 at 23:57:47 UTC, H. S. Teoh wrote: In some Unix terminals, backspace + '_' causes a character to be underlined. So it's really a mini VM, not just pure data. So yeah, the good ole ASCII days never happened. :-D T How can you do that? I'm trying to print the codes

Re: How to print unicode characters (no library)?

2021-12-27 Thread rempas via Digitalmars-d-learn
On Monday, 27 December 2021 at 09:29:38 UTC, Kagamin wrote: D strings are plain arrays without any text-specific logic, the element is called code unit, which has a fixed size, and the array length specifies how many elements are in the array. This model is most adequate for memory correctness,

Re: How to print unicode characters (no library)?

2021-12-27 Thread rempas via Digitalmars-d-learn
On Monday, 27 December 2021 at 14:23:37 UTC, Adam D Ruppe wrote: [...] After reading the whole things, I said it and I'll say it again! You guys must get paid for your support I also helped a guy in another forum yesterday writing a very big reply and tbh it felt great :P (or of course

Re: How to print unicode characters (no library)?

2021-12-27 Thread rempas via Digitalmars-d-learn
On Monday, 27 December 2021 at 14:30:55 UTC, Adam D Ruppe wrote: Most unix things do utf-8 more often than not, but technically you are supposed to check the locale and change the terminal settings to do it right. Cool! I mean, I don't plan on supporting legacy systems so I think we're fine i

Re: How to print unicode characters (no library)?

2021-12-27 Thread rempas via Digitalmars-d-learn
On Monday, 27 December 2021 at 14:47:51 UTC, Kagamin wrote: https://utf8everywhere.org/ - this is an advise from a windows programmer, I use it too. Windows allocates a per thread buffer and when you call, say, WriteConsoleA, it first transcodes the string to UTF-16 in the buffer and calls Writ

Re: How to print unicode characters (no library)?

2021-12-27 Thread rempas via Digitalmars-d-learn
On Monday, 27 December 2021 at 21:38:03 UTC, Era Scarecrow wrote: Well to add functionality with say ANSI you entered an escape code and then stuff like offset, color, effect, etc. UTF-8 automatically has escape codes being anything 128 or over, so as long as the terminal understand it, it sho

Re: How to print unicode characters (no library)?

2021-12-28 Thread rempas via Digitalmars-d-learn
On Tuesday, 28 December 2021 at 12:56:11 UTC, Adam D Ruppe wrote: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html and that's not quite full either. it really is a mess from hell Still less complicated and organized than my life...

Re: How to print unicode characters (no library)?

2021-12-28 Thread rempas via Digitalmars-d-learn
On Tuesday, 28 December 2021 at 13:04:26 UTC, Adam D Ruppe wrote: What is your library? You might be able to just use my terminal.d too My library will be "libd" it will be like "libc" but better and cooler! And it will be native to D! And of course it will not depend on "libc" and it wil

Re: How to print unicode characters (no library)?

2021-12-28 Thread rempas via Digitalmars-d-learn
On Tuesday, 28 December 2021 at 14:53:57 UTC, rempas wrote: On Tuesday, 28 December 2021 at 12:56:11 UTC, Adam D Ruppe wrote: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html and that's not quite full either. it really is a mess from hell Still less complicated and organized than

Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-28 Thread rempas via Digitalmars-d-learn
I would like to know if that's possible. Actually I would like to do something like the following: ``` extern (C) void main() { void print_num(int num, comp_time_type int mul) { static if (is(mul == ten)) { printf("%d\n", num * 10); } else static if (is(mul == three)) { pri

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
On Tuesday, 28 December 2021 at 22:06:50 UTC, Steven Schveighoffer wrote: On 12/28/21 4:19 PM, rempas wrote: Here: ``` extern (C) void main() {   void print_num(int mul)(int num) {     static if (is(mul == ten)) {   printf("%d\n", num * 10);     } else static if (is(mul == three)) {  

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
On Tuesday, 28 December 2021 at 22:26:33 UTC, max haughton wrote: Why do you need this? What's wrong with a normal branch in this case. Runtime performance. I want the value to get checked at compile time and use "static if" with it

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
On Wednesday, 29 December 2021 at 01:34:22 UTC, Stanislav Blinov wrote: One can also do this kind of stuff: ```d import core.stdc.stdio; struct Literal(alias val) { enum value = val; } enum lit(alias val) = Literal!val.init; void print_num(Arg)(int num, Arg mul) { static if (is(Arg =

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
On Wednesday, 29 December 2021 at 11:09:04 UTC, max haughton wrote: If the value is known at compile time the compiler can pretty easily do that for you unless you're really unlucky. How is this even possible?

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
On Wednesday, 29 December 2021 at 14:49:40 UTC, Steven Schveighoffer wrote: On 12/29/21 3:55 AM, rempas wrote: Thanks! That's cool but I don't want this to be this way. Or at least I want it to be able to take a default value so we don't have to get passed all the time. OK: ``` extern (C)

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
On Wednesday, 29 December 2021 at 16:27:22 UTC, max haughton wrote: Inlining + constant propagation. Fancier iterations on those exist too but 90% of the speedup will come from those since for it to matter they likely would've been used in first place. Sounds like black magic? So If I write th

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-29 Thread rempas via Digitalmars-d-learn
On Wednesday, 29 December 2021 at 17:20:59 UTC, max haughton wrote: This is handled by the compiler backend. The simplest way it can do this kind of optimization is by "inlining" the function. This is done by transplanting the function body into the place it's used. At this point the compiler

How to find how many places from left the dot appears in a flot/double?

2021-12-30 Thread rempas via Digitalmars-d-learn
Let's say that I have the following float/double: 137.837 How can I find how many places from left the dot appears? So in this example I want to take either three or four (depending on how you think it). Does anyone knows how I can do that and can explain it to me?

Re: How to find how many places from left the dot appears in a flot/double?

2021-12-30 Thread rempas via Digitalmars-d-learn
On Thursday, 30 December 2021 at 11:17:39 UTC, afg45 wrote: See the https://en.wikipedia.org/wiki/Common_logarithm fpr the explanations. ``` import std.stdio, std.math; void main(string[] args) { alias numDigits = (f) => log10(f + 0.5).ceil(); } ``` Thanks a lot! However, I used the "log

mixin does not work as expected

2022-01-05 Thread rempas via Digitalmars-d-learn
I'm trying to use mixins and enums to "expand" code in place but the results are not what I expected and I'm getting an weird error. I have created the smallest possible example to reproduce the error and it is the following: ``` enum base_digit(string ten, string sixteen, string two, string

Re: mixin does not work as expected

2022-01-05 Thread rempas via Digitalmars-d-learn
On Wednesday, 5 January 2022 at 09:02:53 UTC, vit wrote: Try this: ```d pragma(msg, type_check!("static if", "i8", "true", "5", "4", "10", "5")); ``` Result: ```d static if(is_same!(num, i8)) { mixin(base_digit!("5", "4", "10", "5")); static if (true) { mixin(overflow_check!"i8.

Re: mixin does not work as expected

2022-01-05 Thread rempas via Digitalmars-d-learn
On Wednesday, 5 January 2022 at 09:33:07 UTC, vit wrote: 2) if-else must be in same mixin: ```d mixin("" + type_check!("static if", "i8", "true", "5", "4", "10", "5") + type_check!("else static if", "i16", "true", "7", "6", "18", "8") + type_check!("else static if", "i3

Re: mixin does not work as expected

2022-01-05 Thread rempas via Digitalmars-d-learn
On Wednesday, 5 January 2022 at 10:47:44 UTC, rempas wrote: There may be a problem in the "type_check" enum but I wanted to post this reply before I search any any case there is something else happening and you happen to know. I will update if I found it Well, it is specifically the "static i

Re: mixin does not work as expected

2022-01-05 Thread rempas via Digitalmars-d-learn
On Wednesday, 5 January 2022 at 17:48:03 UTC, H. S. Teoh wrote: Yeah, this won't fly. Whatever you pass to mixin must be one or more *complete* declaration or (possibly compound) statements. It's illegal to pass the `static if` and its else-clause to two different mixin() invocations (they are

Undefined reference to "S_ISDIR" and "S_ISREG" when compiling with "-betterC"

2022-01-10 Thread rempas via Digitalmars-d-learn
I'm having the following code: ```d import core.sys.posix.sys.stat; int is_dir(const char* path) { stat_t s; if (stat(path, &s) == 0) { if (S_ISDIR(s.st_mode)) return 1; else return 0; } else return 2; } int is_regf(const char* path) { stat_t s; if (stat(path, &s) == 0) {

Re: Undefined reference to "S_ISDIR" and "S_ISREG" when compiling with "-betterC"

2022-01-10 Thread rempas via Digitalmars-d-learn
On Monday, 10 January 2022 at 19:28:56 UTC, Adam D Ruppe wrote: You are mistaken, -betterC's main mission is to exclude core. Declarations that just bind to something externally will still work, since it doesn't matter if they're included or not, but this one, being a macro in the original C,

Re: Undefined reference to "S_ISDIR" and "S_ISREG" when compiling with "-betterC"

2022-01-10 Thread rempas via Digitalmars-d-learn
On Monday, 10 January 2022 at 20:11:06 UTC, Adam D Ruppe wrote: On Monday, 10 January 2022 at 19:33:36 UTC, rempas wrote: Thanks! It needs to be linked with which library? That segment of druntime. You can get dmd to do it for you by using the `-i -i=core.sys` flags (or maybe just the -i=cor

Re: Undefined reference to "S_ISDIR" and "S_ISREG" when compiling with "-betterC"

2022-01-10 Thread rempas via Digitalmars-d-learn
On Monday, 10 January 2022 at 20:28:43 UTC, rempas wrote: I see. Thanks! I will see what this function is supposed to do in the "stat.d" file or I'll write assembly and implement the system call be myself. Have a nice day my friend! When never mind, I just copy pasted the macros from "stat.h"

Re: Undefined reference to "S_ISDIR" and "S_ISREG" when compiling with "-betterC"

2022-01-13 Thread rempas via Digitalmars-d-learn
On Monday, 10 January 2022 at 21:17:22 UTC, Adam D Ruppe wrote: It's not a system call, it just a bitflag tester. I wasn't talking about the flag actually but for the "stat" system call

Will it be possible to write a GCC frontend in D?

2022-02-23 Thread rempas via Digitalmars-d-learn
I'm using a book called "modern compiler design (version 2)" to learn how to create compiler and I thought about learning and applying this knowledge on writing a GCC frontend just for fun to see where this gets me. However, I've seen some tutorials and I've seen people doing it in C++. Now, I

Re: Will it be possible to write a GCC frontend in D?

2022-02-23 Thread rempas via Digitalmars-d-learn
On Wednesday, 23 February 2022 at 20:06:58 UTC, bachmeier wrote: Not sure if this is the same thing (a link would have helped) but [this is done in C](https://www.cs.usfca.edu/~galles/compilerdesign/C/csupport.html) Thank you! However, one things that I didn't mentioned is that GCC was once

Re: Will it be possible to write a GCC frontend in D?

2022-02-23 Thread rempas via Digitalmars-d-learn
On Wednesday, 23 February 2022 at 20:19:04 UTC, Adam D Ruppe wrote: I should hope so, otherwise gdc wouldn't exist, yet it does. [extern (C++)](https://github.com/D-Programming-GDC/gdc/tree/master/gcc/d/dmd) and manually creating the decorations. At least that's my understanding of browsing t

Re: Will it be possible to write a GCC frontend in D?

2022-02-23 Thread rempas via Digitalmars-d-learn
On Wednesday, 23 February 2022 at 21:33:00 UTC, Adam D Ruppe wrote: There is no branch, it is just part of the upstream mainline. see: https://gcc.gnu.org/git/?p=gcc.git;a=tree;f=gcc/d/dmd;h=454baa71a0d270fb891acdda6fd0215a3d6cb588;hb=HEAD Oh, this is what I mean by saying "branch" so my mi

Is it safe to read to memory after it has been allocated with `pureMalloc` and `pureRealloc`?

2022-04-04 Thread rempas via Digitalmars-d-learn
In other terms, do these functions auto-initialize memory to be ready for use? I test it out using `printf` to print a string. "%s" expects for a null terminated string and it seems to work so I suppose that these functions auto-initialize the bytes to `\0`. However, memory is tricky and I sti

Re: Is it safe to read to memory after it has been allocated with `pureMalloc` and `pureRealloc`?

2022-04-04 Thread rempas via Digitalmars-d-learn
On Monday, 4 April 2022 at 07:39:08 UTC, Salih Dincer wrote: On Monday, 4 April 2022 at 07:32:00 UTC, rempas wrote: Does anyone knows what's going on here? Source code? Why does it matter? ``` import core.memory; import core.stdc.stdio; import core.stdc.stdlib; extern (C) void main() {

Re: Is it safe to read to memory after it has been allocated with `pureMalloc` and `pureRealloc`?

2022-04-04 Thread rempas via Digitalmars-d-learn
On Monday, 4 April 2022 at 09:26:13 UTC, Stanislav Blinov wrote: No. Neither `malloc` nor `realloc` (for which D's `pure...` variants are mere wrappers) are specified to initialize allocated memory. `calloc`, however, is - it initializes allocated block with zeroes. Thanks, that's what I was

Create an associative array with function pointers as the value

2022-04-20 Thread rempas via Digitalmars-d-learn
I'm trying to create an associative array where the keys will be a "string" type and the values will be function pointers. I'm using a custom type is called "file_struct" and for anyone that wants to try specifically with this type, the definition is the following: ```d struct file_struct {

Re: Create an associative array with function pointers as the value

2022-04-20 Thread rempas via Digitalmars-d-learn
On Wednesday, 20 April 2022 at 11:10:18 UTC, vit wrote: You need shared static this for initializing immutable AA: ```d immutable void function(ref file_struct)[string] common_identifiers; shared static this(){ common_identifiers = [ "let" : &parse_let, // "macro" :

Re: Create an associative array with function pointers as the value

2022-04-20 Thread rempas via Digitalmars-d-learn
On Wednesday, 20 April 2022 at 14:29:33 UTC, rikki cattermole wrote: On 21/04/2022 2:15 AM, rempas wrote: Unfortunately, this will not work for me as it uses "TypeInfo" and it is not available with "-betterC". Thank you for trying to help regardless! You can't use AA's in -betterC. The impl

Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread rempas via Digitalmars-d-learn
I have created a structure that is a actually an array that allocates memory and growths. It is a template and it has worked with a couple of types that I have tried with. It doesn't work with one tho and I cannot understand why. I will list the smallest possible code I could. Keep in mind that

Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread rempas via Digitalmars-d-learn
On Thursday, 5 May 2022 at 11:49:29 UTC, vit wrote: ```d this.capacity += DEF_VEC_REALLOC_SIZE; //realloc(this.ptr, T.sizeof * DEF_VEC_REALLOC_SIZE); this.ptr = realloc(this.ptr, T.sizeof * this.capacity); //<<-- ``` Oh, right! I forgot to say it. I'm using my own `realloc`

Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread rempas via Digitalmars-d-learn
On Thursday, 5 May 2022 at 14:20:49 UTC, Steven Schveighoffer wrote: Your assignment operator does nothing. -Steve Thanks! That was indeed the problem! In the other data structures, it worked without needing explicitly provide one so I didn't thought about it. Thanks a lot, you're saving me!

Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-06 Thread rempas via Digitalmars-d-learn
On Thursday, 5 May 2022 at 21:07:22 UTC, colleen camacho wrote: can i use method of Dynamic array in C using malloc library function. Program example will create an integer array of any length dynamically by asking the array size and array elements from user and display on the screen.how memory

Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-06 Thread rempas via Digitalmars-d-learn
On Friday, 6 May 2022 at 13:35:13 UTC, Steven Schveighoffer wrote: You don't need to declare an assign operator, the default for structs is to member-wise copy all the values. However, if you do provide one, it will be used. Note that a simple default can be done like: ```d ref My_File opAssi

Re: Why is the compiled file size so huge?

2022-05-28 Thread rempas via Digitalmars-d-learn
On Friday, 27 May 2022 at 13:40:25 UTC, Alexander Zhirov wrote: I'm trying to compile a file that weighs 3 kilobytes. I'm also linking a self-written dynamic library. I don't understand why the resulting executable file is so huge? After all, all libraries are present: [...] I did a similar

How to map machine instctions in memory and execute them? (Aka, how to create a loader)

2022-06-06 Thread rempas via Digitalmars-d-learn
I tried to find anything that will show code but I wasn't able to find anything expect for an answer on stackoverflow. I would find a lot of theory but no practical code that works. What I want to do is allocate memory (with execution mapping), add the machine instructions and then allocate ano

Re: How to map machine instctions in memory and execute them? (Aka, how to create a loader)

2022-06-06 Thread rempas via Digitalmars-d-learn
On Monday, 6 June 2022 at 15:27:12 UTC, Alain De Vos wrote: Note , it is also possible to do inline assembly with asm{...} or __asm(T) {..}. Thank you for the info! I am aware of that, I don't want to practically do this. I just want to learn how it works. It will be useful when I'll built m

Re: How to map machine instctions in memory and execute them? (Aka, how to create a loader)

2022-06-06 Thread rempas via Digitalmars-d-learn
On Monday, 6 June 2022 at 16:08:28 UTC, Adam D Ruppe wrote: On a lot of systems, it can't be executable and writable at the same time, it is a security measure. see https://en.wikipedia.org/wiki/W%5EX so you might have to mprotect it to remove the write permission before trying to execute

Re: How to map machine instctions in memory and execute them? (Aka, how to create a loader)

2022-06-06 Thread rempas via Digitalmars-d-learn
On Monday, 6 June 2022 at 16:24:58 UTC, Guillaume Piolat wrote: See: https://github.com/GhostRain0/xbyak https://github.com/MrSmith33/vox/blob/master/source/vox/utils/mem.d Thank you! And I just noticed that the second source is from Vox

Re: How to map machine instctions in memory and execute them? (Aka, how to create a loader)

2022-06-06 Thread rempas via Digitalmars-d-learn
On Monday, 6 June 2022 at 18:05:23 UTC, Johan wrote: This instruction is wrong. Note that you are writing twice to RDX, but also that you are using `mov sign_extend imm32, reg64` instead of `mov imm64, reg64` (`0x48 0xBA`?). Third, why append an extra zero (`*cast(char*)(code + 32) = 0x00;`)? T

Re: How to map machine instctions in memory and execute them? (Aka, how to create a loader)

2022-06-08 Thread rempas via Digitalmars-d-learn
On Monday, 6 June 2022 at 15:13:45 UTC, rempas wrote: In case someone is wondering, I found an answer in another forum. The code is the following: ```d import core.stdc.stdio; import core.stdc.string; import core.stdc.stdlib; import core.sys.posix.sys.mman; void putbytes(char **code, const char

How can I check if a type is a pointer?

2022-06-25 Thread rempas via Digitalmars-d-learn
For example, something like the following: ```d void main() { char accepted_type; char* non_accepted_type; if (__traits(isPointer, typeof(accepted_type))) { // The type is not accepted } else { /* The type is not a pointer so its accepted */ } if (__traits(isPointer, typeof(non_

Re: How can I check if a type is a pointer?

2022-06-25 Thread rempas via Digitalmars-d-learn
On Saturday, 25 June 2022 at 14:32:27 UTC, Ola Fosheim Grøstad wrote: I guess you can look at the source code for https://dlang.org/phobos/std_traits.html#isPointer Thank you! Nice and quick ;) For anyone interested, here's the source code: ```d enum bool isPointer(T) = is(T == U*, U) && __

Re: How can I check if a type is a pointer?

2022-06-25 Thread rempas via Digitalmars-d-learn
On Saturday, 25 June 2022 at 14:51:49 UTC, Paul Backus wrote: Use an [`is()` expression:][1] ```d if (is(typeof(accepted_type) == T*, T)) { // it's a pointer } ``` In English, you read this as "if `typeof(accepted_type)` matches the pattern `T*`, where `T` is a type." If you want to lea

How can I match every instance of a template type (struct)?

2022-07-12 Thread rempas via Digitalmars-d-learn
I want to do something like the following: ```d import core.stdc.stdio; Test!(T, "mode1") make_test(T)(T data) { Test!(T, "mode1") t = { data }; return t; } struct Test(T, string mode = "ref") { T data; } extern (C) void main() { auto obj = make_test(20); static if (is(typeof(obj) ==

Re: How can I match every instance of a template type (struct)?

2022-07-12 Thread rempas via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 13:37:49 UTC, ag0aep6g wrote: static if (is(typeof(obj) == Test!T, T)) { printf("YES!!!\n"); } Haah? Ok, what does this work anyway? I thought you needed parenthesis for more than 1 templated arguments...

Re: How can I match every instance of a template type (struct)?

2022-07-12 Thread rempas via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 13:34:42 UTC, rempas wrote: [...] Thank you all for your help! @Ali Çehreli That makes things much much easier! I'll look at the source code in "traits.d" and I'll copy-paste it into my library ;)

How do I initialize a templated constructor?

2022-08-07 Thread rempas via Digitalmars-d-learn
In the following struct (as an example, not real code): ``` struct TestArray(ulong element_n) { int[element_n] elements; this(string type)(ulong number) { pragma(msg, "The type is: " ~ typeof(type).stringof); } } ``` I want to create it and be able to successfully initialize the temp

Re: CMake and D

2022-08-07 Thread rempas via Digitalmars-d-learn
On Saturday, 6 August 2022 at 18:22:45 UTC, Jan Allersma wrote: I figured out a strategy to solve te problem: 1) Create a C++ function which will be called in D. 2) Build a static C++ library with CMake and add dependencies (In my case: SDL libraries) 3) Create a new project (`dub init`). 4)

Re: Hacking C code vs D code

2022-08-07 Thread rempas via Digitalmars-d-learn
On Thursday, 4 August 2022 at 23:11:36 UTC, pascal111 wrote: One of problems faced me in C programming is hacking data with C code that some hackers do with C code which make me needs more tools to protect my C code, but I don't have good resources in my current time, while I noticed that D cod

Re: How do I initialize a templated constructor?

2022-08-08 Thread rempas via Digitalmars-d-learn
On Monday, 8 August 2022 at 06:58:42 UTC, bauss wrote: ``` this(string type)(ulong number) { ``` You cannot do this. Instead your type should look like this: First let's change it up a little bit. ``` struct TestArray(ulong element_n, string type) { int[element_n] elements; this(ulong n

Re: How do I initialize a templated constructor?

2022-08-08 Thread rempas via Digitalmars-d-learn
On Monday, 8 August 2022 at 08:27:49 UTC, bauss wrote: Yeah I think the only template argument you can have for constructors are `this` which will refer to things like the class that inherited the current class etc. not sure what else, but you can't really pass anything to it yourself unfortu

Re: How do I initialize a templated constructor?

2022-08-08 Thread rempas via Digitalmars-d-learn
On Monday, 8 August 2022 at 11:03:21 UTC, Dom Disc wrote: But if you only want to know the type of the parameter, you can do this: ```D struct TestArray(ulong element_n) { int[element_n] elements; this(type)(type number) { pragma(msg, "The type is: " ~ type.stringof); } } ``` U

Re: How do I initialize a templated constructor?

2022-08-08 Thread rempas via Digitalmars-d-learn
Thank you all for the info! I'll try to find another way to do it as it is not possible to match the exact behavior I want! Have a great day everyone!

How ptr arithmitic works??? It doesn't make any sense....

2022-12-04 Thread rempas via Digitalmars-d-learn
First a little bit of theory. A pointer just points to a memory address which is a number. So when I add "10" to this pointer, it will point ten bytes after the place it was pointing to, right? Another thing with pointers is that it doesn't have "types". A pointer always just points to a locati

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-04 Thread rempas via Digitalmars-d-learn
On Sunday, 4 December 2022 at 16:40:17 UTC, ag0aep6g wrote: Not quite. Adding 10 to a T* means adding 10 * T.sizeof. Oh! I thought it was addition. Is there a specific reasoning for that if you are aware of?

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-04 Thread rempas via Digitalmars-d-learn
On Sunday, 4 December 2022 at 17:27:39 UTC, Nick Treleaven wrote: On Sunday, 4 December 2022 at 16:33:35 UTC, rempas wrote: (MemoryBlock.sizeof is 16 on my 64-bit system). The above adds 16 bytes to ptr. The above adds 16 * MemoryBlock.sizeof bytes (16 * 16) to ptr, because ptr is cast first.

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-05 Thread rempas via Digitalmars-d-learn
On Sunday, 4 December 2022 at 19:00:15 UTC, H. S. Teoh wrote: This is true only if you're talking about pointers in the sense of pointers in assembly language. Languages like C and D add another layer of abstraction over this. Another thing with pointers is that it doesn't have "types". T

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-05 Thread rempas via Digitalmars-d-learn
On Monday, 5 December 2022 at 08:21:44 UTC, bauss wrote: Because it's much easier to work with. Ex. if you have an array of 4 signed 32 bit integers that you're pointing to then you can simply just increment the pointer by 1. If it was raw bytes then you'd have to increment the pointer by

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-05 Thread rempas via Digitalmars-d-learn
On Monday, 5 December 2022 at 18:01:47 UTC, ag0aep6g wrote: You can use bracket notation with pointers. You just need to move your closing parenthesis a bit. Assuming that `ptr` is a `void*`, these are all equivalent: ```d (cast(int*) ptr)[i] = whatever; *((cast(int*) ptr) + i) = whatever; *

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-06 Thread rempas via Digitalmars-d-learn
On Monday, 5 December 2022 at 22:21:06 UTC, Salih Dincer wrote: Yeah, there is such a thing! I'm sure you'll all like this example: [...] Great example! Thank you my friend!

How would the equivalent C type be in D?

2023-03-01 Thread rempas via Digitalmars-d-learn
I'm looking into [this](https://www.x.org/releases/X11R7.7/doc/libxcb/tutorial/index.html) tutorial to learn XCB and I'm trying to write the code in D with betterC. In the section 9.1 (sorry, I cannot give a section link, the article does not give us this ability), I'm facing a problem and my prog

Re: How would the equivalent C type be in D?

2023-03-01 Thread rempas via Digitalmars-d-learn
On Wednesday, 1 March 2023 at 08:26:07 UTC, FeepingCreature wrote: 11 is SIGSEGV. A segfault, or access violation, happens when you try to access unallocated memory. In this case, let me annotate your code so it's easier to see what's happening: ```d // null is the default value for a pointe

Re: How would the equivalent C type be in D?

2023-03-07 Thread rempas via Digitalmars-d-learn
On Tuesday, 7 March 2023 at 09:05:45 UTC, FeepingCreature wrote: Yay! Yes, that is a bit weird. First of all, the actual signal is 11 [...] Thank you for the info!

Is there a way to get the name of the current function?

2023-03-07 Thread rempas via Digitalmars-d-learn
For example, in the given code: ```d void my_function() { import std.stdio; writeln("The name of the function is: ", ); } ``` Is there something to put in the place of `` to get the name of the function?

Re: Is there a way to get the name of the current function?

2023-03-07 Thread rempas via Digitalmars-d-learn
On Tuesday, 7 March 2023 at 22:19:22 UTC, JG wrote: Yes, see here: https://dlang.org/spec/expression.html#specialkeywords OMG, so that's when they are located! I was trying to search the spec and find them in different pages but had no luck! Thanks a lot for the help, have a great day!

Re: Is there a way to get the name of the current function?

2023-03-07 Thread rempas via Digitalmars-d-learn
On Tuesday, 7 March 2023 at 22:28:41 UTC, ryuukk_ wrote: ``` import std.stdio; void main() { writeln("file:", __FILE__); writeln("function is: ", __FUNCTION__); writeln("function is: ", __PRETTY_FUNCTION__ ); } $ dmd -run tester.d file:tester.d function is: tester.m

How can I get the scalar type of a pointer to pointer (and in even deeper levels)

2023-03-11 Thread rempas via Digitalmars-d-learn
Let's see the following code: ```d void test_fn(T)(T val) { pragma(msg, "The type of the pointer is: " ~ typeof(*val).stringof); } extern (C) void main() { int* int_ptr = cast(int*)0x1037; char* char_ptr = cast(char*)0x1037; test_fn(int_ptr); test_fn(char_ptr); } ``` This function

Re: @nogc and Phobos

2023-03-11 Thread rempas via Digitalmars-d-learn
On Saturday, 11 March 2023 at 12:04:25 UTC, bomat wrote: Hello! First of all, let's start by making clear of what `@nogc` means. It it an attribute that is used one function signatures and it annotates that the garbage collector will not be used inside this function. However, in the scenario t

Re: How can I get the scalar type of a pointer to pointer (and in even deeper levels)

2023-03-11 Thread rempas via Digitalmars-d-learn
On Saturday, 11 March 2023 at 12:59:59 UTC, ag0aep6g wrote: alias Foo(T : U*, U) = Foo!U; alias Foo(T) = T; static assert(is(Foo!(int*) == int)); static assert(is(Foo!(int**) == int)); static assert(is(Foo!(int***) == int)); static assert(is(Foo!(int) == int)); Wait, but "Foo" is defined t

Re: How can I get the scalar type of a pointer to pointer (and in even deeper levels)

2023-03-11 Thread rempas via Digitalmars-d-learn
On Saturday, 11 March 2023 at 13:37:26 UTC, ag0aep6g wrote: On 11.03.23 14:22, rempas wrote: On Saturday, 11 March 2023 at 12:59:59 UTC, ag0aep6g wrote: alias Foo(T : U*, U) = Foo!U; alias Foo(T) = T; static assert(is(Foo!(int*) == int)); static assert(is(Foo!(int**) == int)); static assert(is

Re: @nogc and Phobos

2023-03-11 Thread rempas via Digitalmars-d-learn
On Saturday, 11 March 2023 at 16:21:40 UTC, bomat wrote: first: I didn't want to sound aggressive or like I was trying to bash D, sorry if it came across like that. Oh don't worry! People have openly criticized (and still doing) the language before. It's nothing to worry, criticism help us be

Re: @nogc and Phobos

2023-04-12 Thread rempas via Digitalmars-d-learn
On Tuesday, 14 March 2023 at 20:40:39 UTC, bomat wrote: Sounds intriguing. Anything I can look at yet? Or still all top secret? :) Oh, amazing! I let you on waiting for almost a month and I wouldn't had see it if I didn't came to post another question. I'm so so sorry for the waiting,

How can a function pointer required to be extern(C)?

2023-04-12 Thread rempas via Digitalmars-d-learn
Sorry if the title doesn't make any sense, let me explain. So, I do have the following code that does not compile: ```d import core.sys.posix.pthread; /* The library */ struct Thread { private: pthread_t thread_id; public: this(void* function(void*) func, void* arg = null, scope const(pth

Re: How can a function pointer required to be extern(C)?

2023-04-13 Thread rempas via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 20:36:59 UTC, H. S. Teoh wrote: IMO this is a bug either in D's syntax or in the parser. I'd file an enhancement request. In the meantime, you can use alias as a workaround: ---snip--- extern(C) void* abc(void*) {return null;} alias FuncPtr = typeof(

Re: How can a function pointer required to be extern(C)?

2023-04-13 Thread rempas via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 21:00:04 UTC, John Chapman wrote: You can also express it like this: ```d extern(C) alias FuncPtr = void* function(void*); ``` Thank you! This is how I was planning to do anyway because other that the fact that I like the syntax of that a little bit more, this

Re: How does D’s ‘import’ work?

2023-06-02 Thread rempas via Digitalmars-d-learn
On Wednesday, 31 May 2023 at 18:43:52 UTC, Cecil Ward wrote: Is there an explanation of how D’s ‘import’ works somewhere? I’m trying to understand the comparison with the inclusion of .h files, similarities if any and differences with the process. I do wonder why no-one have linked the [Module

Re: How does D’s ‘import’ work?

2023-06-02 Thread rempas via Digitalmars-d-learn
On Wednesday, 31 May 2023 at 18:43:52 UTC, Cecil Ward wrote: Is there an explanation of how D’s ‘import’ works somewhere? I’m trying to understand the comparison with the inclusion of .h files, similarities if any and differences with the process. I do wonder why no-one have linked the [Module

<    1   2   3   >