Re: Output Range Problem? How to make it work?

2021-10-10 Thread Paul Backus via Digitalmars-d-learn
On Monday, 11 October 2021 at 00:19:44 UTC, apz28 wrote: /* Getting this error onlineapp.d(34): Error: none of the overloads of `toString` are callable using argument types `(Buffer)`, candidates are: onlineapp.d(19):`onlineapp.Foo.toString()` onlineapp.d(26):

Output Range Problem? How to make it work?

2021-10-10 Thread apz28 via Digitalmars-d-learn
import std.range.primitives: isOutputRange; @safe: struct Buffer { @safe: void put(char c) {} void put(scope const(char)[] s) {} } struct Foo { @safe: string toString() {return null;} ref Writer

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread rempas via Digitalmars-d-learn
On Sunday, 10 October 2021 at 14:00:37 UTC, Elmar wrote: On Sunday, 10 October 2021 at 13:56:06 UTC, rempas wrote: Actually I know about BetterC and how to call C functions from D and visa versa. I would also disagree that "BetterC" is almost no improvement over C as about 90% of the

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread Adam Ruppe via Digitalmars-d-learn
On Sunday, 10 October 2021 at 13:52:57 UTC, Elmar wrote: The language subset "BetterC" is required for calling D functions from C though. This is false. You can use any D features when calling it from C, you just need to provide an init and term function that is called from C that runtime

Re: What is the proper way to outline static-if-conditions ?

2021-10-10 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 15:15:51 UTC, drug wrote: It would be nice if one could use pattern-matching for it in D. Is this possible? As I know it's impossible, but you can use a regular template: ... If anyone is interested in pattern matching, someone provides a package "dpmatch"

Re: What is the proper way to outline static-if-conditions ?

2021-10-10 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 15:15:51 UTC, drug wrote: As I know it's impossible, but you can use a regular template: ```d template isPointedStaticArray(T) { static if (isPointer!T) enum isPointedStaticArray = isStaticArray!(PointerTarget!T); else enum

Re: What is the proper way to outline static-if-conditions ?

2021-10-10 Thread drug via Digitalmars-d-learn
On 10.10.2021 18:01, Elmar wrote: Well, I just wondered why your code would compile and mine wouldn't. The `version(all)` variant will not compile on my computer with `rdmd` because `PointerTarget` only allows pointers. It depends on compiler version. This variant is compiled on version

Re: What is the proper way to outline static-if-conditions ?

2021-10-10 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 15:01:17 UTC, Elmar wrote: ```d enum isPointedStaticArray(T) = is(PointerTarget!T : P[N], P, size_t N); ``` ```d enum isPointedStaticArray(X : P*, P) = .isStaticArray!(PointerTarget!X); ``` `isStaticArray` is a good example that makes me ask how to

Re: What is the proper way to outline static-if-conditions ?

2021-10-10 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 14:36:50 UTC, Elmar wrote: On Sunday, 10 October 2021 at 14:08:13 UTC, drug wrote: You just need to check if T is a pointer: ```D import std; alias DA = int[]; alias SA = int[3]; alias PSA = SA*; alias PDA = DA*; version(all) enum isPointedStaticArray(T) =

Re: Traits in a template enum

2021-10-10 Thread Mike Parker via Digitalmars-d-learn
On Sunday, 10 October 2021 at 12:56:30 UTC, Some Guy wrote: But I did not understand what you meant by "enums hold values, not types". Aren't types values at compile time? Types can be template arguments, if that's what you mean, but they aren't values.

Re: What is the proper way to outline static-if-conditions ?

2021-10-10 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 14:08:13 UTC, drug wrote: You just need to check if T is a pointer: ```D import std; alias DA = int[]; alias SA = int[3]; alias PSA = SA*; alias PDA = DA*; version(all) enum isPointedStaticArray(T) = isPointer!T && isStaticArray!(PointerTarget!T); else enum

Re: What is the proper way to outline static-if-conditions ?

2021-10-10 Thread drug via Digitalmars-d-learn
You just need to check if T is a pointer: ```D import std; alias DA = int[]; alias SA = int[3]; alias PSA = SA*; alias PDA = DA*; version(all) enum isPointedStaticArray(T) = isPointer!T && isStaticArray!(PointerTarget!T); else enum isPointedStaticArray(T) = isPointer!T &&

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 13:56:06 UTC, rempas wrote: Actually I know about BetterC and how to call C functions from D and visa versa. I would also disagree that "BetterC" is almost no improvement over C as about 90% of the language is there!! C++ classes are also supported Nice :-) ,

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 13:52:57 UTC, Elmar wrote: The language subset "BetterC" is required for calling D functions from C though. Unfortunately, the runtime features of BetterC are limited and some of C's language features aren't availabe like C99 variable-length-arrays. "BetterC" is

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread rempas via Digitalmars-d-learn
On Sunday, 10 October 2021 at 13:52:57 UTC, Elmar wrote: Hopefully it will :-) . D has some good C support. You can call any C function from `D` by declaring it `extern(C) `. The language subset "BetterC" is required for calling D functions from C though. Unfortunately, the runtime

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 13:10:27 UTC, rempas wrote: Thanks, I'm converting a library from C to D so I have to fix all the other bugs first to see If it's working but probably it will. Have an amazing day my friend! Hopefully it will :-) . D has some good C support. You can call any C

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread rempas via Digitalmars-d-learn
On Sunday, 10 October 2021 at 11:26:18 UTC, Elmar wrote: Hello rempas. This is the way: ```d import core.stdc.stdlib : malloc, free; extern(C) void* function(ulong) mallocPointer = extern(C) void function(void*) freePointer = ``` `function` in the type is already a function pointer. Not

Re: What is a "comma expression"?

2021-10-10 Thread rempas via Digitalmars-d-learn
On Sunday, 10 October 2021 at 12:19:39 UTC, Adam D Ruppe wrote: On Sunday, 10 October 2021 at 12:01:33 UTC, rempas wrote: [...] The comma expression in C is a combination of two things but in the context of another thing. Well that's not very good, but like there's statements and

Re: What is a "comma expression"?

2021-10-10 Thread rempas via Digitalmars-d-learn
On Sunday, 10 October 2021 at 12:13:47 UTC, Elmar wrote: Hello rempas. The comma-operator `,` is like `;` but results in an expression value, not a statement like `;` would. The left side of `,` is executed like a statement and the value of the right side of `,` is returned.

Re: What is the proper way to outline static-if-conditions ?

2021-10-10 Thread Elmar via Digitalmars-d-learn
PS: the title is a misnomer. `is(T : P*, P) && isStaticArray!P` doesn't either compile when inlined because `P` is not defined when not matched.

What is the proper way to outline static-if-conditions ?

2021-10-10 Thread Elmar via Digitalmars-d-learn
Hey D people. Currently in my project I have worked on a unified type interface for all arrays which requires fixed-size arrays to be stored as pointer (in correspondence to dynamic and associative arrays) and allow them being allocated with any selected allocator. There can be code like

Re: Traits in a template enum

2021-10-10 Thread Some Guy via Digitalmars-d-learn
On Sunday, 10 October 2021 at 12:48:49 UTC, Adam D Ruppe wrote: On Sunday, 10 October 2021 at 12:39:17 UTC, Some Guy wrote: I have this enum to get the type enums hold values, not types. try alias instead Thanks! `alias typeOfMember(T, string member) = typeof(__traits(getMember, T,

Re: Traits in a template enum

2021-10-10 Thread Adam D Ruppe via Digitalmars-d-learn
On Sunday, 10 October 2021 at 12:39:17 UTC, Some Guy wrote: I have this enum to get the type enums hold values, not types. try alias instead

Re: Traits in a template enum

2021-10-10 Thread Some Guy via Digitalmars-d-learn
It actually looks like I'm having problems wherever I try to pass that enum as a template parameter.

Traits in a template enum

2021-10-10 Thread Some Guy via Digitalmars-d-learn
I have this enum to get the type of a member field in a struct: `enum typeOfMember(T, string member) = typeof(__traits(getMember, T, member));` I'm having problems when I try to used it though. For example: ```D writeln(typeOfMember!(T, member).stringof); // Doesn't work: Error: initializer

Re: What is a "comma expression"?

2021-10-10 Thread Adam D Ruppe via Digitalmars-d-learn
On Sunday, 10 October 2021 at 12:01:33 UTC, rempas wrote: return *ret = v, 1; The comma expression in C is a combination of two things but in the context of another thing. Well that's not very good, but like there's statements and expressions. Statements do something but do not have a

Re: What is a "comma expression"?

2021-10-10 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 12:01:33 UTC, rempas wrote: This is the first time I'm finding something like that. I'm having the following code in C: ``` if (0 == (i >> 7)) { if (len < 1) return -1; v = i; return *ret = v, 1; } ``` This is part of a function that returns an

What is a "comma expression"?

2021-10-10 Thread rempas via Digitalmars-d-learn
This is the first time I'm finding something like that. I'm having the following code in C: ``` if (0 == (i >> 7)) { if (len < 1) return -1; v = i; return *ret = v, 1; } ``` This is part of a function that returns an `int`. When I'm running this in C, it works. However in D,

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 10:44:15 UTC, rempas wrote: I'm having the following C code: ``` static void* (*ppmalloc)(size_t) = malloc; static void (*ppfree)(void*) = free; ``` I want to covert this code in D so I try to do the following: ``` static void* function(size_t)*ppmalloc = malloc;

How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread rempas via Digitalmars-d-learn
I'm having the following C code: ``` static void* (*ppmalloc)(size_t) = malloc; static void (*ppfree)(void*) = free; ``` I want to covert this code in D so I try to do the following: ``` static void* function(size_t)*ppmalloc = malloc; static void function(void*)*ppfree = free; ``` If I do

Re: How to "stringnify"?

2021-10-10 Thread rempas via Digitalmars-d-learn
On Sunday, 10 October 2021 at 08:54:55 UTC, evilrat wrote: That's probably depends on what you are trying to achieve. If you want to write code-like string that looks like a code you can use token strings https://dlang.org/spec/lex.html#token_strings ```d string code = q{ float sqr(float

Re: How to "stringnify"?

2021-10-10 Thread rempas via Digitalmars-d-learn
On Sunday, 10 October 2021 at 08:48:21 UTC, Imperatorn wrote: For simple stuff you have .stringof Thanks!

Re: How to "stringnify"?

2021-10-10 Thread evilrat via Digitalmars-d-learn
On Sunday, 10 October 2021 at 08:28:30 UTC, rempas wrote: Is there a way to "stringnify" in Dlang? In C we would do something like the following: `#define STRINGIFY(x) #x` What's the equivalent in D? That's probably depends on what you are trying to achieve. If you want to write code-like

Re: How to "stringnify"?

2021-10-10 Thread Imperatorn via Digitalmars-d-learn
On Sunday, 10 October 2021 at 08:28:30 UTC, rempas wrote: Is there a way to "stringnify" in Dlang? In C we would do something like the following: `#define STRINGIFY(x) #x` What's the equivalent in D? For simple stuff you have .stringof

How to "stringnify"?

2021-10-10 Thread rempas via Digitalmars-d-learn
Is there a way to "stringnify" in Dlang? In C we would do something like the following: `#define STRINGIFY(x) #x` What's the equivalent in D?