Re: Unexpected result comparing to null

2021-08-23 Thread jfondren via Digitalmars-d-learn
On Monday, 23 August 2021 at 13:00:36 UTC, DLearner wrote: Hi The code below compiles and runs producing 'Not null'. ``` void main() { import std.stdio; int Var1; int* ptrVar; ptrVar = if (ptrVar == null) { writeln("Null"); } else { writeln

Unexpected result comparing to null

2021-08-23 Thread DLearner via Digitalmars-d-learn
Hi The code below compiles and runs producing 'Not null'. ``` void main() { import std.stdio; int Var1; int* ptrVar; ptrVar = if (ptrVar == null) { writeln("Null"); } else { writeln("Not null"); } } ``` However, should it

Re: How to check if variable of some type can be of null value?

2021-07-25 Thread Adam D Ruppe via Digitalmars-d-learn
On Saturday, 24 July 2021 at 18:10:07 UTC, Alexey wrote: The goal I with to achieve by this check - is to use template and to assign value to variable basing on it's ability to accept null as a value. The most direct representation of that is __traits(compiles, (T t) { t = null

Re: How to check if variable of some type can be of null value?

2021-07-24 Thread JG via Digitalmars-d-learn
On Saturday, 24 July 2021 at 20:10:37 UTC, JG wrote: On Saturday, 24 July 2021 at 19:39:02 UTC, Alexey wrote: [...] There are probably better ways. However, this seems to work: ```d import std; enum canBeSetToNull(T) = __traits(compiles,(T.init is null)); interface I1 { } class C1 : I1

Re: How to check if variable of some type can be of null value?

2021-07-24 Thread JG via Digitalmars-d-learn
On Saturday, 24 July 2021 at 19:39:02 UTC, Alexey wrote: On Saturday, 24 July 2021 at 18:10:07 UTC, Alexey wrote: I've tried to use ```typeof(t) is cast(t)null```, but compiler exits with error and so this can't be used for checking this issue. The goal I with to achieve by this check

Re: How to check if variable of some type can be of null value?

2021-07-24 Thread Alexey via Digitalmars-d-learn
On Saturday, 24 July 2021 at 18:10:07 UTC, Alexey wrote: I've tried to use ```typeof(t) is cast(t)null```, but compiler exits with error and so this can't be used for checking this issue. The goal I with to achieve by this check - is to use template and to assign value to variable basing

How to check if variable of some type can be of null value?

2021-07-24 Thread Alexey via Digitalmars-d-learn
I've tried to use ```typeof(t) is cast(t)null```, but compiler exits with error and so this can't be used for checking this issue. The goal I with to achieve by this check - is to use template and to assign value to variable basing on it's ability to accept null as a value. some testing

Re: How do I check if a type is assignable to null at compile time?

2021-02-28 Thread Jack via Digitalmars-d-learn
> > > enum isAssignableNull(T) = is(T : Object) || isPointer(T); > > > > but how do I cover all cases? > > You can check if it's null with this `variable is null` and > you can test it with assert as in `assert(variable is null);` I mean a give type T n

Re: How do I check if a type is assignable to null at compile time?

2021-02-26 Thread H. S. Teoh via Digitalmars-d-learn
l(T) = is(T : Object) || isPointer(T); > > > > > > but how do I cover all cases? > > > > You can check if it's null with this `variable is null` and you > > can test it with assert as in `assert(variable is null);` > > I mean a give type T not variable

Re: How do I check if a type is assignable to null at compile time?

2021-02-26 Thread Jack via Digitalmars-d-learn
On Friday, 26 February 2021 at 23:37:18 UTC, Murilo wrote: On Friday, 26 February 2021 at 05:25:14 UTC, Jack wrote: I started with: enum isAssignableNull(T) = is(T : Object) || isPointer(T); but how do I cover all cases? You can check if it's null with this `variable is null` and you can

Re: How do I check if a type is assignable to null at compile time?

2021-02-26 Thread Murilo via Digitalmars-d-learn
On Friday, 26 February 2021 at 05:25:14 UTC, Jack wrote: I started with: enum isAssignableNull(T) = is(T : Object) || isPointer(T); but how do I cover all cases? You can check if it's null with this `variable is null` and you can test it with assert as in `assert(variable is null);`

Re: How do I check if a type is assignable to null at compile time?

2021-02-26 Thread Jack via Digitalmars-d-learn
? Something like this should work: enum isAssignableNull(T) = __traits(compiles, (T t) => t = null); `isAssignableNull!(immutable void*)` is true with his definition but false with yours. Of course you are correct that you cannot assign to an immutable pointer. yep, it must be true for point

Re: How do I check if a type is assignable to null at compile time?

2021-02-25 Thread Nathan S. via Digitalmars-d-learn
) = __traits(compiles, (T t) => t = null); `isAssignableNull!(immutable void*)` is true with his definition but false with yours. Of course you are correct that you cannot assign to an immutable pointer.

Re: How do I check if a type is assignable to null at compile time?

2021-02-25 Thread Nathan S. via Digitalmars-d-learn
On Friday, 26 February 2021 at 05:25:14 UTC, Jack wrote: I started with: enum isAssignableNull(T) = is(T : Object) || isPointer(T); but how do I cover all cases? If I understand what you mean by "is assignable to null", this should do it: --- enum bool isAssignableNull(T) = is(t

Re: How do I check if a type is assignable to null at compile time?

2021-02-25 Thread Paul Backus via Digitalmars-d-learn
On Friday, 26 February 2021 at 05:25:14 UTC, Jack wrote: I started with: enum isAssignableNull(T) = is(T : Object) || isPointer(T); but how do I cover all cases? Something like this should work: enum isAssignableNull(T) = __traits(compiles, (T t) => t = null);

How do I check if a type is assignable to null at compile time?

2021-02-25 Thread Jack via Digitalmars-d-learn
I started with: enum isAssignableNull(T) = is(T : Object) || isPointer(T); but how do I cover all cases?

Re: null and initialized string comparisons

2021-02-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/17/21 4:16 PM, Adam D. Ruppe wrote: On Wednesday, 17 February 2021 at 20:48:22 UTC, Martin wrote: is this how it supposed to be? (https://run.dlang.io/is/7B4irm) == compares contents. Both null and "" have empty contents and are interchangable for operators that work o

Re: null and initialized string comparisons

2021-02-17 Thread Adam D. Ruppe via Digitalmars-d-learn
On Wednesday, 17 February 2021 at 20:48:22 UTC, Martin wrote: is this how it supposed to be? (https://run.dlang.io/is/7B4irm) == compares contents. Both null and "" have empty contents and are interchangable for operators that work on contents. The assert just looks at t

null and initialized string comparisons

2021-02-17 Thread Martin via Digitalmars-d-learn
Hi, is this how it supposed to be? (https://run.dlang.io/is/7B4irm) --- string a = null; string t = ""; assert( ! a ); assert( t ); assert( t == a ); --- I have not expected assert(t == a) to be true - i would like to know the argument for why this is correct when at the same ti

Re: Open question: what code pattern you use usually for null safety problem

2021-01-15 Thread Basile B. via Digitalmars-d-learn
On Thursday, 14 January 2021 at 18:24:44 UTC, ddcovery wrote: I know there is other threads about null safety and the "possible" ways to support this in D and so on. [...] If it's not a bother, I'd like to know how you usually approach it [...] Thanks!!! I have a opDispatch sol

Re: Open question: what code pattern you use usually for null safety problem

2021-01-15 Thread Dukc via Digitalmars-d-learn
that *person*, or its *father* property can be null Probably the incremental check solution. A helper function if I find myself doing that more than two or three times. On the other hand, I don't have to do this that often. I usually design the functions to either except non-null values, or to

Re: Open question: what code pattern you use usually for null safety problem

2021-01-15 Thread ddcovery via Digitalmars-d-learn
On Friday, 15 January 2021 at 14:25:09 UTC, Steven Schveighoffer wrote: On 1/15/21 9:19 AM, Steven Schveighoffer wrote: Something similar to BlackHole or WhiteHole. Essentially there's a default action for null for all types/fields/methods, and everything else is passed through. And now

Re: Open question: what code pattern you use usually for null safety problem

2021-01-15 Thread ddcovery via Digitalmars-d-learn
)() if (__traits(hasMember, T, mem)) {    alias Ret = typeof(() { return __traits(getMember, *_val, mem); }());    if(_val is null) return NullCheck!(Ret)(null);    else return NullCheck!(Ret)(__trats(getMember, *_val, mem));    }    bool opCast(V: bool)() { return _val !is null

Re: Open question: what code pattern you use usually for null safety problem

2021-01-15 Thread Imperatorn via Digitalmars-d-learn
On Friday, 15 January 2021 at 14:25:09 UTC, Steven Schveighoffer wrote: On 1/15/21 9:19 AM, Steven Schveighoffer wrote: Something similar to BlackHole or WhiteHole. Essentially there's a default action for null for all types/fields/methods, and everything else is passed through. And now

Re: Open question: what code pattern you use usually for null safety problem

2021-01-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/15/21 9:19 AM, Steven Schveighoffer wrote: Something similar to BlackHole or WhiteHole. Essentially there's a default action for null for all types/fields/methods, and everything else is passed through. And now reading the other thread about this above, it looks like this type

Re: Open question: what code pattern you use usually for null safety problem

2021-01-15 Thread Steven Schveighoffer via Digitalmars-d-learn
__traits(getMember, *_val, mem); }());    if(_val is null) return NullCheck!(Ret)(null);    else return NullCheck!(Ret)(__trats(getMember, *_val, mem));    }    bool opCast(V: bool)() { return _val !is null; } } auto nullCheck(T)(T *val) { return AutoNullCheck!T(val);} // usage

Re: Open question: what code pattern you use usually for null safety problem

2021-01-15 Thread ddcovery via Digitalmars-d-learn
On Thursday, 14 January 2021 at 18:24:44 UTC, ddcovery wrote: I know there is other threads about null safety and the "possible" ways to support this in D and so on. This is only an open question to know what code patterns you usually use to solve this situation in D I

Re: Open question: what code pattern you use usually for null safety problem

2021-01-14 Thread ddcovery via Digitalmars-d-learn
On Thursday, 14 January 2021 at 20:35:49 UTC, Dennis wrote: On Thursday, 14 January 2021 at 18:24:44 UTC, ddcovery wrote: If it's not a bother, I'd like to know how you usually approach it Usually I don't deal with null because my functions get primitive types, slices, or structs. `ref

Re: Open question: what code pattern you use usually for null safety problem

2021-01-14 Thread ddcovery via Digitalmars-d-learn
); }()); if(_val is null) return NullCheck!(Ret)(null); else return NullCheck!(Ret)(__trats(getMember, *_val, mem)); } bool opCast(V: bool)() { return _val !is null; } } auto nullCheck(T)(T *val) { return AutoNullCheck!T(val);} // usage if(nullCheck(person).father.father

Re: Anything in D to avoid check for null everywhere?

2021-01-14 Thread ddcovery via Digitalmars-d-learn
On Thursday, 14 January 2021 at 21:49:41 UTC, Christian Köstlin wrote: ... Did you have a look at https://code.dlang.org/packages/optional? Especially https://aliak00.github.io/optional/optional/oc/oc.html might go in the right direction. Kind regards, Christian Thats nice!!! I was

Re: Open question: what code pattern you use usually for null safety problem

2021-01-14 Thread ddcovery via Digitalmars-d-learn
like xml or json data that may be missing. Yes, this is the usual situation (Personally, I use "DTO" structured objects... that are serialized/unserialized to JSON) So I just special cased those. My json lib doesn't return null per se, it returns var(null) which is allowed to j

Re: Anything in D to avoid check for null everywhere?

2021-01-14 Thread Christian Köstlin via Digitalmars-d-learn
On 12.01.21 22:37, Jack wrote: I was looking for a way to avoid null checks everywhere. I was checking the Null object pattern, or use something like enforce pattern, or even if I could make a new operator and implement something like C#'s .? operator, that Java was going to have one

Re: Open question: what code pattern you use usually for null safety problem

2021-01-14 Thread Dennis via Digitalmars-d-learn
On Thursday, 14 January 2021 at 18:24:44 UTC, ddcovery wrote: If it's not a bother, I'd like to know how you usually approach it Usually I don't deal with null because my functions get primitive types, slices, or structs. `ref` parameters can be used to replace pointers that may not be null

Re: Open question: what code pattern you use usually for null safety problem

2021-01-14 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/14/21 1:24 PM, ddcovery wrote: I know there is other threads about null safety and the "possible" ways to support this in D and so on. This is only an open question to know what code patterns you usually use to solve this situation in D:   if(person.father.father.name

Re: Open question: what code pattern you use usually for null safety problem

2021-01-14 Thread mw via Digitalmars-d-learn
On Thursday, 14 January 2021 at 18:24:44 UTC, ddcovery wrote: I know there is other threads about null safety and the "possible" ways to support this in D and so on. This is only an open question to know what code patterns you usually use to solve this situ

Re: Open question: what code pattern you use usually for null safety problem

2021-01-14 Thread Adam D. Ruppe via Digitalmars-d-learn
cased those. My json lib doesn't return null per se, it returns var(null) which is allowed to just return more harmless nulls. Thus you write `person.father.father.name.get!string` and it will be empty if anything was null in the chain. With dom, you can optionSelector("person > father

Open question: what code pattern you use usually for null safety problem

2021-01-14 Thread ddcovery via Digitalmars-d-learn
I know there is other threads about null safety and the "possible" ways to support this in D and so on. This is only an open question to know what code patterns you usually use to solve this situation in D: if(person.father.father.name == "Peter") doSomething(); if(pe

Re: Anything in D to avoid check for null everywhere?

2021-01-13 Thread ddcovery via Digitalmars-d-learn
On Tuesday, 12 January 2021 at 21:37:11 UTC, Jack wrote: I was looking for a way to avoid null checks everywhere. I was checking the Null object pattern, or use something like enforce pattern, or even if I could make a new operator and implement something like C#'s .? operator, that Java

Re: Anything in D to avoid check for null everywhere?

2021-01-13 Thread ddcovery via Digitalmars-d-learn
On Wednesday, 13 January 2021 at 09:02:37 UTC, ddcovery wrote: Find more details here: https://run.dlang.io/gist/392c06e745d1a35df71084ce4d29fed7 Ups... it seems that the link is not working (it is the first time I try to generate a dalng/gist link... I'm not sure if this can really be

Re: Anything in D to avoid check for null everywhere?

2021-01-13 Thread ddcovery via Digitalmars-d-learn
On Tuesday, 12 January 2021 at 21:37:11 UTC, Jack wrote: I was looking for a way to avoid null checks everywhere. I was checking the Null object pattern, or use something like enforce pattern, or even if I could make a new operator and implement something like C#'s .? operator, that Java

Re: Member variables in method are null when called as delegate from thread

2021-01-13 Thread Arafel via Digitalmars-d-learn
is going null or anything. But a strange problem that I can't seem to debug. The output is at the bottom. import vibe.db.mongo.mongo; import core.thread; import std.stdio; void main(){     auto callable = new Callable

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread tsbockman via Digitalmars-d-learn
On Wednesday, 13 January 2021 at 02:15:49 UTC, Tim wrote: Basically, the program calls a function which modifies a document in the database. If it is called form it's own class' constructor, it works fine. If it is called by a thread, it never returns. ... class Caller : Thread{ void

Re: Anything in D to avoid check for null everywhere?

2021-01-12 Thread Jack via Digitalmars-d-learn
Currently I'm with this: auto ref ifNotNull(T, T2)(T lhs, lazy T2 rhs) { if(lhs) { rhs(); } return lhs; } far from good. I wish there was a way to create a new operator so I would make .? similar to C#'s but would evaluate the left-handed side only if it's null and do

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread Tim via Digitalmars-d-learn
ram calls a function which modifies a document in the database. If it is called form it's own class' constructor, it works fine. If it is called by a thread, it never returns. I don't think that a member variable is going null or anything. But a strange problem that I can't seem to debug. The out

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread tsbockman via Digitalmars-d-learn
On Tuesday, 12 January 2021 at 14:00:11 UTC, Steven Schveighoffer wrote: On 1/11/21 8:49 PM, tsbockman wrote: However, this re-ordering IS permitted to freely alter the behavior of your code from the perspective of OTHER threads. A likely cause of your bug is that the write to db by the

Anything in D to avoid check for null everywhere?

2021-01-12 Thread Jack via Digitalmars-d-learn
I was looking for a way to avoid null checks everywhere. I was checking the Null object pattern, or use something like enforce pattern, or even if I could make a new operator and implement something like C#'s .? operator, that Java was going to have one but they refused[1] (doesn't behave

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/11/21 8:49 PM, tsbockman wrote: On Monday, 11 January 2021 at 00:43:00 UTC, Tim wrote: When MessageService calls the delegate for start, db is null. If I call start() in the Foo constructor it works just fine. Am I missing something here? Do delegates get called outside of their class

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/11/21 12:26 PM, Arafel wrote: Thanks for the detailed explanation! I think this mixing of types and storage classes makes a very unfortunate combination: ``` import std; int i = 0; shared int j = 0; struct S {     int i = 0;     shared int j = 0; } S s; void main() {     i = 1;    

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/11/21 6:52 PM, Paul Backus wrote: On Monday, 11 January 2021 at 16:10:49 UTC, Steven Schveighoffer wrote: There are some... odd rules. struct S { [...]    immutable int e = 5; // stored in data segment, not per instance! Are you sure? struct S {     immutable int n = 123;    

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread Imperatorn via Digitalmars-d-learn
On Monday, 11 January 2021 at 17:26:00 UTC, Arafel wrote: void f() { assert(i == 0); // Expected assert(j == 1); // Expected assert(s.i == 0); // Expected assert(s.j == 0); // Wait, what? } At first sight this looks unexpected. But I think if you have a shared variable inside

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread tsbockman via Digitalmars-d-learn
On Monday, 11 January 2021 at 00:43:00 UTC, Tim wrote: When MessageService calls the delegate for start, db is null. If I call start() in the Foo constructor it works just fine. Am I missing something here? Do delegates get called outside of their class context? I know I could just pass the db

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Paul Backus via Digitalmars-d-learn
On Monday, 11 January 2021 at 16:10:49 UTC, Steven Schveighoffer wrote: There are some... odd rules. struct S { [...] immutable int e = 5; // stored in data segment, not per instance! Are you sure? struct S { immutable int n = 123; this(int n) { this.n = n; } } void main() {

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Arafel via Digitalmars-d-learn
On 11/1/21 17:10, Steven Schveighoffer wrote: A shared member is a sharable member of the class. It does not put the item in global storage. There are some... odd rules. struct S {    static int a; // TLS    shared static int b; // shared data storage    shared int c; // local variable,

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/11/21 10:42 AM, Arafel wrote: On 11/1/21 14:42, Steven Schveighoffer wrote: That isn't exactly true. Member variables are members of the object. If the object is shared, the member variables are shared. If the object is local the variables are local. Thread local really only applies

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Arafel via Digitalmars-d-learn
On 11/1/21 14:42, Steven Schveighoffer wrote: That isn't exactly true. Member variables are members of the object. If the object is shared, the member variables are shared. If the object is local the variables are local. Thread local really only applies to *static* variables, such as

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Steven Schveighoffer via Digitalmars-d-learn
legate for start, db is null. If I call start() in the Foo constructor it works just fine. Am I missing something here? Do delegates get called outside of their class context? I know I could just pass the db into start but I want to work out exactly why this is happening Thanks in advance H

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Tim via Digitalmars-d-learn
On Monday, 11 January 2021 at 08:21:21 UTC, Arafel wrote: It's also possible that you'll have to make Foo itself `shared`, or at least convert your constructor into a `shared this ()` to get a shared instance that you can pass to a different thread, but I'm not sure how function pointers /

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Arafel via Digitalmars-d-learn
rvice(8081, commands);     }     void start(){     // Do something with db     } MessageService is a thread that deals with socket communication. When a command comes in, it calls the appropriate delegate given to it by commands. When MessageService calls the delegate for start, db is null. If I cal

Re: Member variables in method are null when called as delegate from thread

2021-01-10 Thread Tim via Digitalmars-d-learn
Ok, so it seems that it isn't null now. But I stall can't call db.getCollection().findAndModify() from vibe.d successfully here. Works just fine in the constructor. When it's called form the MessengerService thread it never returns from the function call

Member variables in method are null when called as delegate from thread

2021-01-10 Thread Tim via Digitalmars-d-learn
art(){ // Do something with db } MessageService is a thread that deals with socket communication. When a command comes in, it calls the appropriate delegate given to it by commands. When MessageService calls the delegate for start, db is null. If I call start() in the Foo constructor it w

Re: Distinguish between a null array and an empty array

2020-05-26 Thread Dukc via Digitalmars-d-learn
On Sunday, 24 May 2020 at 12:29:23 UTC, bauss wrote: Dang, that sucks there is no proper way and I would say that's a big flaw of D. Because what I need it for is for some data serialization but if the value is an empty array then it should be present and if it's null then it should

Re: Distinguish between a null array and an empty array

2020-05-26 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/24/20 8:12 AM, bauss wrote: Is there a way to do that? Since the following are both true: int[] a = null; int[] b = []; assert(a is null); assert(!a.length); assert(b is null); assert(!b.length); What I would like is to tell that b is an empty array and a is a null array. The issue

Re: Distinguish between a null array and an empty array

2020-05-26 Thread WebFreak001 via Digitalmars-d-learn
and if it's null then it should not be present. Since null is used to say "ignore this" in the data serialization. You can use std.typecons.Nullable (or a similar wrapper) to add an extra "ignore this" value to a type. and just in case you use vibe.d serialization (JSON, B

Re: Distinguish between a null array and an empty array

2020-05-25 Thread Nathan S. via Digitalmars-d-learn
On Sunday, 24 May 2020 at 12:12:31 UTC, bauss wrote: Is there a way to do that? Since the following are both true: int[] a = null; int[] b = []; assert(a is null); assert(!a.length); assert(b is null); assert(!b.length); What I would like is to tell that b is an empty array and a is a null

Re: Distinguish between a null array and an empty array

2020-05-24 Thread ag0aep6g via Digitalmars-d-learn
On 24.05.20 14:29, bauss wrote: Dang, that sucks there is no proper way and I would say that's a big flaw of D. Because what I need it for is for some data serialization but if the value is an empty array then it should be present and if it's null then it should not be present. Since null

Re: Distinguish between a null array and an empty array

2020-05-24 Thread ag0aep6g via Digitalmars-d-learn
On 24.05.20 14:12, bauss wrote: Is there a way to do that? Since the following are both true: int[] a = null; int[] b = []; assert(a is null); assert(!a.length); assert(b is null); assert(!b.length); What I would like is to tell that b is an empty array and a is a null array. No way

Re: Distinguish between a null array and an empty array

2020-05-24 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, May 24, 2020 6:12:31 AM MDT bauss via Digitalmars-d-learn wrote: > Is there a way to do that? > > Since the following are both true: > > int[] a = null; > int[] b = []; > > assert(a is null); > assert(!a.length); > > assert(b is null); > asse

Re: Distinguish between a null array and an empty array

2020-05-24 Thread bauss via Digitalmars-d-learn
On Sunday, 24 May 2020 at 12:26:42 UTC, ag0aep6g wrote: On 24.05.20 14:12, bauss wrote: Is there a way to do that? Since the following are both true: int[] a = null; int[] b = []; assert(a is null); assert(!a.length); assert(b is null); assert(!b.length); What I would like is to tell

Re: Distinguish between a null array and an empty array

2020-05-24 Thread bauss via Digitalmars-d-learn
On Sunday, 24 May 2020 at 12:12:31 UTC, bauss wrote: Is there a way to do that? Since the following are both true: int[] a = null; int[] b = []; assert(a is null); assert(!a.length); assert(b is null); assert(!b.length); What I would like is to tell that b is an empty array and a is a null

Distinguish between a null array and an empty array

2020-05-24 Thread bauss via Digitalmars-d-learn
Is there a way to do that? Since the following are both true: int[] a = null; int[] b = []; assert(a is null); assert(!a.length); assert(b is null); assert(!b.length); What I would like is to tell that b is an empty array and a is a null array.

Re: Allocating an empty non null associative arary

2020-03-31 Thread Steven Schveighoffer via Digitalmars-d-learn
On 3/31/20 9:41 AM, Superstar64 wrote: On Tuesday, 31 March 2020 at 06:51:16 UTC, WebFreak001 wrote: This doesn't only happen with null, you will notice that that function will eventually not add anything if you only add values. This is because by adding values you might reallocate the map

Re: Allocating an empty non null associative arary

2020-03-31 Thread Steven Schveighoffer via Digitalmars-d-learn
On 3/31/20 2:51 AM, WebFreak001 wrote: On Tuesday, 31 March 2020 at 02:51:11 UTC, Superstar64 wrote: I want to be modify an associative array by reference from another function. However null associative arrays are pass by value. How do I generically create an empty associative array

Re: Allocating an empty non null associative arary

2020-03-31 Thread Superstar64 via Digitalmars-d-learn
On Tuesday, 31 March 2020 at 06:51:16 UTC, WebFreak001 wrote: This doesn't only happen with null, you will notice that that function will eventually not add anything if you only add values. This is because by adding values you might reallocate the map at some other place in memory and because

Re: Allocating an empty non null associative arary

2020-03-31 Thread WebFreak001 via Digitalmars-d-learn
On Tuesday, 31 March 2020 at 02:51:11 UTC, Superstar64 wrote: I want to be modify an associative array by reference from another function. However null associative arrays are pass by value. How do I generically create an empty associative array? --- import std.stdio; void addElement(int[int

Re: Allocating an empty non null associative arary

2020-03-31 Thread Vladimir Panteleev via Digitalmars-d-learn
On Tuesday, 31 March 2020 at 02:51:11 UTC, Superstar64 wrote: How do I generically create an empty associative array? If you can't pass it by ref, then adding and then removing an element is the only way I know. /// Ensure that arr is non-null if empty. V[K] nonNull(K, V)(V[K] aa

Re: Allocating an empty non null associative arary

2020-03-30 Thread Mike Parker via Digitalmars-d-learn
On Tuesday, 31 March 2020 at 02:51:11 UTC, Superstar64 wrote: I want to be modify an associative array by reference from another function. However null associative arrays are pass by value. How do I generically create an empty associative array? --- import std.stdio; void addElement(int[int

Allocating an empty non null associative arary

2020-03-30 Thread Superstar64 via Digitalmars-d-learn
I want to be modify an associative array by reference from another function. However null associative arrays are pass by value. How do I generically create an empty associative array? --- import std.stdio; void addElement(int[int] data){ data[0] = 0; } void nonempty() { int

Re: Empty string vs null

2020-02-04 Thread mark via Digitalmars-d-learn
Thanks for that thorough and careful explanation. Since I'm trying to learn to write D in good style and want my code to be reliable and maintainable, I've now switched to using "" rather than null.

Re: Empty string vs null

2020-02-04 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, February 4, 2020 12:33:42 AM MST mark via Digitalmars-d-learn wrote: > I have just discovered that D seems to treat empty and null > strings as the same thing: > > // test.d > import std.stdio; > import std.string; > void main() > { > string

Re: Empty string vs null

2020-02-03 Thread JN via Digitalmars-d-learn
On Tuesday, 4 February 2020 at 07:44:08 UTC, mark wrote: Just found this post by Mark Parker that explains: https://forum.dlang.org/post/gvveit$10i5$1...@digitalmars.com I recommend using Nullable from https://dlang.org/phobos/std_typecons.html#Nullable if you want to explicitly allow a

Re: Empty string vs null

2020-02-03 Thread mark via Digitalmars-d-learn
Just found this post by Mark Parker that explains: https://forum.dlang.org/post/gvveit$10i5$1...@digitalmars.com // test.d import std.stdio; import std.string; void main() { report(null, "null"); report(""); report("x"); } void report(const st

Empty string vs null

2020-02-03 Thread mark via Digitalmars-d-learn
I have just discovered that D seems to treat empty and null strings as the same thing: // test.d import std.stdio; import std.string; void main() { string x = null; writeln("x = \"", x, "\""); writeln("null = ", x == null); writeln(&q

Re: if (X !is null && X.Y !is null) access crash

2019-06-10 Thread Amex via Digitalmars-d-learn
On Monday, 10 June 2019 at 19:48:18 UTC, Steven Schveighoffer wrote: On 6/9/19 1:25 AM, Amex wrote: On Saturday, 8 June 2019 at 20:44:13 UTC, Steven Schveighoffer wrote: Try GC.addRef on the X reference, and see if it helps. This is during shutdown so I imagine simply turning off the GC

Re: if (X !is null && X.Y !is null) access crash

2019-06-10 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/9/19 1:25 AM, Amex wrote: On Saturday, 8 June 2019 at 20:44:13 UTC, Steven Schveighoffer wrote: Try GC.addRef on the X reference, and see if it helps. This is during shutdown so I imagine simply turning off the GC should work fine? That way it prevents all cases rather than having to

Re: if (X !is null && X.Y !is null) access crash

2019-06-08 Thread Amex via Digitalmars-d-learn
think of is that something is happening in between the two checks since there is no way it could happen otherwise. Possible the GC cleaned up the object already. When this happens, you get this kind of behavior (the GC intentionally sets the vtable to null to prevent invalid access

Re: if (X !is null && X.Y !is null) access crash

2019-06-08 Thread Steven Schveighoffer via Digitalmars-d-learn
since there is no way it could happen otherwise. Possible the GC cleaned up the object already. When this happens, you get this kind of behavior (the GC intentionally sets the vtable to null to prevent invalid access). Try GC.addRef on the X reference, and see if it helps. -Steve

Re: if (X !is null && X.Y !is null) access crash

2019-06-08 Thread Amex via Digitalmars-d-learn
On Friday, 7 June 2019 at 16:09:47 UTC, Adam D. Ruppe wrote: It happens when I close down my app. is this inside a destructor? No, it's in an external thread(it is in a callback). All I can think of is that something is happening in between the two checks since there is no way it could

Re: if (X !is null && X.Y !is null) access crash

2019-06-07 Thread Adam D. Ruppe via Digitalmars-d-learn
It happens when I close down my app. is this inside a destructor?

Re: if (X !is null && X.Y !is null) access crash

2019-06-07 Thread Amex via Digitalmars-d-learn
On Friday, 7 June 2019 at 14:07:34 UTC, KnightMare wrote: On Friday, 7 June 2019 at 09:26:52 UTC, Amex wrote: if (X !is null && X.Y !is null) access crash is crashing. imo this code is valid. u can write shorter if (X && X.Y) probably crashed in some another place

Re: if (X !is null && X.Y !is null) access crash

2019-06-07 Thread KnightMare via Digitalmars-d-learn
On Friday, 7 June 2019 at 09:26:52 UTC, Amex wrote: if (X !is null && X.Y !is null) access crash is crashing. imo this code is valid. u can write shorter if (X && X.Y) probably crashed in some another place (X is not objRef but something else.. some code later at same line.. dunno)

if (X !is null && X.Y !is null) access crash

2019-06-07 Thread Amex via Digitalmars-d-learn
I don't understand why if (X !is null && X.Y !is null) access crash is crashing. It is true that it is being used in a thread. It happens when I close down my app. The whole point of the check is to make sure X is not null but it seems to be failing. The debugger is showing X is

Re: Very simple null reference escape

2019-06-02 Thread Basile B. via Digitalmars-d-learn
On Sunday, 2 June 2019 at 07:55:27 UTC, Amex wrote: A.B If A is null, crash. A?.B : writeln("HAHA"); No crash, ignored, equivalent to if (A is null) writeln("HAHA"); else A.B; safeAccess from iz does this : https://github.com/Basile-z/iz/blob/master/import/iz/sugar.d#L1666

Re: Very simple null reference escape

2019-06-02 Thread Amex via Digitalmars-d-learn
On Sunday, 2 June 2019 at 14:37:48 UTC, Paul Backus wrote: On Sunday, 2 June 2019 at 07:55:27 UTC, Amex wrote: A.B If A is null, crash. A?.B : writeln("HAHA"); No crash, ignored, equivalent to if (A is null) writeln("HAHA"); else A.B; The "optional" pack

Re: Very simple null reference escape

2019-06-02 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 2 June 2019 at 07:55:27 UTC, Amex wrote: A.B If A is null, crash. A?.B : writeln("HAHA"); No crash, ignored, equivalent to if (A is null) writeln("HAHA"); else A.B; The "optional" package on dub [1] has a .dispatch method that

Very simple null reference escape

2019-06-02 Thread Amex via Digitalmars-d-learn
A.B If A is null, crash. A?.B : writeln("HAHA"); No crash, ignored, equivalent to if (A is null) writeln("HAHA"); else A.B;

Re: Create object from a library's Class returns Null

2019-05-29 Thread dangbinghoo via Digitalmars-d-learn
change gwlib buildtype to sourceLibrary solves the problem, but it should work for static or shared library.

Re: Create object from a library's Class returns Null

2019-05-28 Thread dangbinghoo via Digitalmars-d-learn
On Wednesday, 29 May 2019 at 05:04:54 UTC, dangbinghoo wrote: On Wednesday, 29 May 2019 at 02:42:23 UTC, Adam D. Ruppe wrote: Object.factory is pretty unreliable and has few supporters among the developers. I wouldn't suggest relying on it and instead building your own factory functions. oh,

Re: Create object from a library's Class returns Null

2019-05-28 Thread dangbinghoo via Digitalmars-d-learn
On Wednesday, 29 May 2019 at 02:42:23 UTC, Adam D. Ruppe wrote: Object.factory is pretty unreliable and has few supporters among the developers. I wouldn't suggest relying on it and instead building your own factory functions. oh, that's bad news, but the hibernated library is using this

Re: Create object from a library's Class returns Null

2019-05-28 Thread Adam D. Ruppe via Digitalmars-d-learn
Object.factory is pretty unreliable and has few supporters among the developers. I wouldn't suggest relying on it and instead building your own factory functions.

Re: Create object from a library's Class returns Null

2019-05-28 Thread dangbinghoo via Digitalmars-d-learn
. This seems to be a serious problem, I found it works when I using Ldc2 1.12 version even on ARM target. hi there, Anyone help with this? Not a typo problem, even this --- writeln(Object.factory(ns.toString())); --- result in `null`. just try the github demo project: https

  1   2   3   4   5   6   7   8   >