Re: is it possible synchronized(null) ? i.e NO-OP

2022-08-26 Thread Ali Çehreli via Digitalmars-d-learn
On the main forum, Paul Backus proposed a nested function as well as a scoped lock. On 8/26/22 10:13, mw wrote: >Object lock = (a particular condition) ? realLock : null; And I want to point out that "a particular condition" must not change between the check above and

is it possible synchronized(null) ? i.e NO-OP

2022-08-26 Thread mw via Digitalmars-d-learn
Hi, I haven't tried, but can I do: ``` void foo(lots of params) { Object lock = (a particular condition) ? realLock : null; synchronized(lock) { // lots of complex code block here } } ``` i.e depending on a a particular condition, the complex code block either need to be sync

Re: null == "" is true?

2022-07-20 Thread Antonio via Digitalmars-d-learn
On Wednesday, 20 July 2022 at 13:35:14 UTC, Kagamin wrote: On Tuesday, 19 July 2022 at 18:05:34 UTC, Antonio wrote: In a relational database, `NULL` is not the same that `""`... and `NULL` is not the same that `0`. Are semantically different and there are database invariants (li

Re: null == "" is true?

2022-07-20 Thread Kagamin via Digitalmars-d-learn
On Tuesday, 19 July 2022 at 18:05:34 UTC, Antonio wrote: In a relational database, `NULL` is not the same that `""`... and `NULL` is not the same that `0`. Are semantically different and there are database invariants (like foreign keys) based on it. Trying to "mix

Re: null == "" is true?

2022-07-20 Thread Antonio via Digitalmars-d-learn
On Tuesday, 19 July 2022 at 16:55:39 UTC, Kagamin wrote: As I understand, in your scenario there's no difference between null string and empty string, they both work like empty string, and D treats them as empty string. That's what I mean when I said that distinction between null and empty

Re: null == "" is true?

2022-07-19 Thread Antonio via Digitalmars-d-learn
On Tuesday, 19 July 2022 at 17:05:27 UTC, Kagamin wrote: Also what's the difference between null and empty phone number? In a relational database, `NULL` is not the same that `""`... and `NULL` is not the same that `0`. Are semantically different and there are database invari

Re: null == "" is true?

2022-07-19 Thread Kagamin via Digitalmars-d-learn
Also what's the difference between null and empty phone number?

Re: null == "" is true?

2022-07-19 Thread Kagamin via Digitalmars-d-learn
On Tuesday, 19 July 2022 at 10:29:40 UTC, Antonio wrote: The summary is that a DTO that works like a Map needs to represent the absent key ant this is not the same that the Null value Example: ```d struct Null { /*...*/ } struct Undefined { /*...*/ } struct ContactDto { DtoVal!(Undefined

Re: null == "" is true?

2022-07-19 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Tuesday, 19 July 2022 at 15:30:30 UTC, Bienlein wrote: If the destination of a carrier was set to null, it implied that the destination was currently undefined. Then the robot brought the carrier to some rack where it was put aside for a while till the planning system had created a new

Re: null == "" is true?

2022-07-19 Thread Bienlein via Digitalmars-d-learn
why? Because an empty string is, by default, represented by an empty slice of the null pointer. I don't program in D. I just read from time to time posts in the D forum because of the good quality of what people write. So, I'm not proficient in D, but in general internals should not boil

Re: null == "" is true?

2022-07-19 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Tuesday, 19 July 2022 at 10:29:40 UTC, Antonio wrote: NULL is not the same that UNDEFINED The distintion is really important: NULL is a valid value (i.e.: The person phonenumber is NULL in database)... Of course, you can represent this concept natively in you language (Nullable, Optional

Re: null == "" is true?

2022-07-19 Thread Antonio via Digitalmars-d-learn
On Tuesday, 19 July 2022 at 08:10:25 UTC, Kagamin wrote: On Monday, 18 July 2022 at 21:23:32 UTC, Antonio wrote: I will study it in detail and report (if required). May be, I will write the DTO problem with D article if I find time in august. In my experience null and empty in DTOs usually

Re: null == "" is true?

2022-07-19 Thread Kagamin via Digitalmars-d-learn
On Monday, 18 July 2022 at 21:23:32 UTC, Antonio wrote: I will study it in detail and report (if required). May be, I will write the DTO problem with D article if I find time in august. In my experience null and empty in DTOs usually play the same logical role. It's a very contrived

Re: null == "" is true?

2022-07-18 Thread Antonio via Digitalmars-d-learn
On Monday, 18 July 2022 at 17:20:04 UTC, Kagamin wrote: ... If you want such difference, use the Nullable wrapper or Algebraic. I do :-) In fact, I use algebraic types supporting Null and Undefined for DTOs representation (and REST APIs). But I discovered some "rare" si

Re: null == "" is true?

2022-07-18 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Monday, 18 July 2022 at 17:20:04 UTC, Kagamin wrote: Difference between null and empty is useless. Not really. `null` typically means that the value is missing, irrelevant and not usable, which is quite different from having "" as a usable value.

Re: null == "" is true?

2022-07-18 Thread Kagamin via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 20:36:03 UTC, Antonio wrote: Honestly, it is difficult to understand for newcomers... there is a reason, but there is a reason in javascript for `0 == ''` too People would have different preferences there. Difference between null and empty is useless. D does

Re: null == "" is true?

2022-07-15 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 15 July 2022 at 11:12:07 UTC, Steven Schveighoffer wrote: Note that the term `null` and `[]` are special tokens that morph type to whatever is most appropriate at the time. `null` implicitly can be typed as any pointer type, or any array type. `[]` can be typed as any array type

Re: null == "" is true?

2022-07-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On Friday, 15 July 2022 at 06:38:58 UTC, Salih Dincer wrote: Consider null type array which is a related topic but it cannot get a null element! The first is ok, but the second is legal. So no effect, is it normal? ```d auto p = [ null, null ];//* assert( is(typeof(null

Re: null == "" is true?

2022-07-15 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 22:58:32 UTC, Steven Schveighoffer wrote: ```d string a = "abcabc"; assert(a[0 .. 3] == a[3 .. $]) assert(a[0 .. 3] !is a[3 .. $]) ``` The point is, `==` compares *value*, `is` always compares *identity*. Consider null type array which is a rel

Re: null == "" is true?

2022-07-14 Thread Palak via Digitalmars-d-learn
Hi @Steven Schveighoffer, Yes solution looking useful and sure it will work. Thanks.

Re: null == "" is true?

2022-07-12 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/12/22 4:36 PM, Antonio wrote: On Tuesday, 12 July 2022 at 18:56:43 UTC, Paul Backus wrote: On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote: Because an empty string is, by default, represented by an empty slice of the null pointer. Do not rely on this, however; it's possible

Re: null == "" is true?

2022-07-12 Thread Antonio via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 20:36:03 UTC, Antonio wrote: Honestly, it is difficult to understand for newcomers... there is a reason, but there is a reason in javascript for `0 == ''` too Correction ```d string a = null; assert(a is null); assert(a == ""); string b = ""

Re: null == "" is true?

2022-07-12 Thread Antonio via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 18:56:43 UTC, Paul Backus wrote: On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote: Because an empty string is, by default, represented by an empty slice of the null pointer. Do not rely on this, however; it's possible sometimes to get an empty string

Re: null == "" is true?

2022-07-12 Thread user1234 via Digitalmars-d-learn
an array. Never use null. use `[]` (empty array literal). Just to be clear: `[]` and `null` are the exact same thing (null pointer, zero length). The reason to prefer `[]` over `null` is purely for readability. The meaning is exactly the same. ah yes. The case I thought to was actually ```d

Re: null == "" is true?

2022-07-12 Thread ag0aep6g via Digitalmars-d-learn
On 12.07.22 22:14, H. S. Teoh wrote: Pedantically, no, they're not the same. You can assign null to a pointer, but you can't assign [] to a pointer. `null` is a supertype of `[]`. But probably nobody actually cares about this distinction. :-D If we're ignoring context, "null"

Re: null == "" is true?

2022-07-12 Thread H. S. Teoh via Digitalmars-d-learn
> Absolutely. I'd like to add: especially as default parameter value > > that's an array. Never use null. use `[]` (empty array literal). > > Just to be clear: `[]` and `null` are the exact same thing (null > pointer, zero length). The reason to prefer `[]` over `null` is purely &g

Re: null == "" is true?

2022-07-12 Thread ag0aep6g via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 19:02:01 UTC, user1234 wrote: On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote: [...] Do not rely on this, however; Absolutely. I'd like to add: especially as default parameter value that's an array. Never use null. use `[]` (empty array literal

Re: null == "" is true?

2022-07-12 Thread user1234 via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote: On Tue, Jul 12, 2022 at 04:27:44PM +, Antonio via Digitalmars-d-learn wrote: It works ```d void main() { assert(null==""); } ``` why? Because an empty string is, by default, represented by an empty slice of the nu

Re: null == "" is true?

2022-07-12 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote: Because an empty string is, by default, represented by an empty slice of the null pointer. Do not rely on this, however; it's possible sometimes to get an empty string that isn't null, e.g., if you incrementally shrink a slice over

Re: null == "" is true?

2022-07-12 Thread Ali Çehreli via Digitalmars-d-learn
On 7/12/22 10:11, Steven Schveighoffer wrote: > The algorithm to compare *any* arrays is first verify the lengths are > the same. Then for each element in the array, compare them. Since there > are 0 elements in both the empty string and the null string, they are > equal. Che

Re: null == "" is true?

2022-07-12 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/12/22 12:40 PM, H. S. Teoh wrote: Because an empty string is, by default, represented by an empty slice of the null pointer. No, it's not a null pointer. It's a pointer to a zero-character. But it is indeed an empty slice. -Steve

Re: null == "" is true?

2022-07-12 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/12/22 12:27 PM, Antonio wrote: It works ```d void main() {    assert(null==""); } ``` why? A string is not exactly a reference type. It's a length and a pointer. This can be confusing to newcomers, especially ones that come from languages that treat arrays and strings

Re: null == "" is true?

2022-07-12 Thread Hipreme via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote: On Tue, Jul 12, 2022 at 04:27:44PM +, Antonio via Digitalmars-d-learn wrote: It works ```d void main() { assert(null==""); } ``` why? Because an empty string is, by default, represented by an empty slice of the nu

Re: null == "" is true?

2022-07-12 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jul 12, 2022 at 04:27:44PM +, Antonio via Digitalmars-d-learn wrote: > It works > > ```d > void main() > { >assert(null==""); > } > ``` > > why? Because an empty string is, by default, represented by an empty slice of the null poin

null == "" is true?

2022-07-12 Thread Antonio via Digitalmars-d-learn
It works ```d void main() { assert(null==""); } ``` why?

Re: Null terminated character

2022-07-02 Thread Bastiaan Veelo via Digitalmars-d-learn
On Thursday, 23 June 2022 at 16:16:26 UTC, vc wrote: I've try this '\0'*10 and didn't work, i want the results be \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 ```d string nulls = '\0'.repeat(10).array; ``` — Bastiaan.

Re: How to check if something can be null

2022-07-01 Thread Antonio via Digitalmars-d-learn
On Friday, 1 July 2022 at 15:35:00 UTC, Adam D Ruppe wrote: On Friday, 1 July 2022 at 13:48:25 UTC, Antonio wrote: I has been using this pattern each time something needs special treatment when it can be null: i'd prolly check `static if(is(typeof(null) : T))` which means if the null literal

Re: How to check if something can be null

2022-07-01 Thread Adam D Ruppe via Digitalmars-d-learn
On Friday, 1 July 2022 at 13:48:25 UTC, Antonio wrote: I has been using this pattern each time something needs special treatment when it can be null: i'd prolly check `static if(is(typeof(null) : T))` which means if the null literal implicitly converts to type T. there's also the bludgeon

Re: How to check if something can be null

2022-07-01 Thread user1234 via Digitalmars-d-learn
On Friday, 1 July 2022 at 13:53:28 UTC, Antonio wrote: On Friday, 1 July 2022 at 13:48:25 UTC, Antonio wrote: -Why? I realized Json is an struct (not an object)... and I supose, it is managing null asignation manually (as a way to build Json(null)). -Whats the correct whay to test

Re: How to check if something can be null

2022-07-01 Thread Antonio via Digitalmars-d-learn
On Friday, 1 July 2022 at 13:48:25 UTC, Antonio wrote: -Why? I realized Json is an struct (not an object)... and I supose, it is managing null asignation manually (as a way to build Json(null)). -Whats the correct whay to test if something can be null? That's my question :-p

How to check if something can be null

2022-07-01 Thread Antonio via Digitalmars-d-learn
I has been using this pattern each time something needs special treatment when it can be null: ```d void doSomething(T)(T v) { import std.traits: isAssignable; static if( isAssignable!(T, typeof(null))) { if(v is null) writeln("This is null"); else

Re: Null terminated character

2022-06-23 Thread frame via Digitalmars-d-learn
On Thursday, 23 June 2022 at 17:27:51 UTC, frame wrote: On Thursday, 23 June 2022 at 16:16:26 UTC, vc wrote: I've try this '\0'*10 and didn't work, i want the results be \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 One way: ```d import std.range; repeat("\0", 10).join(""); ``` If you just

Re: Null terminated character

2022-06-23 Thread frame via Digitalmars-d-learn
On Thursday, 23 June 2022 at 16:16:26 UTC, vc wrote: I've try this '\0'*10 and didn't work, i want the results be \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 One way: ```d import std.range; repeat("\0", 10).join(""); ```

Null terminated character

2022-06-23 Thread vc via Digitalmars-d-learn
I've try this '\0'*10 and didn't work, i want the results be \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

Re: Enforce not null at compile time?

2022-06-20 Thread Antonio via Digitalmars-d-learn
On Monday, 20 June 2022 at 19:08:32 UTC, max haughton wrote: On Monday, 20 June 2022 at 17:48:48 UTC, Antonio wrote: Is there any way to specify that a variable, member or parameter can't be null? You can use an invariant if it's a member of an aggregate but be warned that these are only

Re: Enforce not null at compile time?

2022-06-20 Thread max haughton via Digitalmars-d-learn
On Monday, 20 June 2022 at 17:48:48 UTC, Antonio wrote: Is there any way to specify that a variable, member or parameter can't be null? You can use an invariant if it's a member of an aggregate but be warned that these are only checked at the boundaries of public member functions.

Re: Enforce not null at compile time?

2022-06-20 Thread Dennis via Digitalmars-d-learn
On Monday, 20 June 2022 at 17:48:48 UTC, Antonio wrote: Is there any way to specify that a variable, member or parameter can't be null? Depends on the type. Basic types can't be null. Pointers and classes can always be `null`, though you could wrap them in a custom library type that doesn't

Enforce not null at compile time?

2022-06-20 Thread Antonio via Digitalmars-d-learn
Is there any way to specify that a variable, member or parameter can't be null?

Re: How to print or check if a string is "\0" (null) terminated in the D programming language?

2022-04-06 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 6 April 2022 at 08:55:43 UTC, BoQsc wrote: I have a feeling that some parts of my code contains unterminated strings and they do overflow into other string [...] If you suspect overflow, you can try string wrapping.

Re: How to print or check if a string is "\0" (null) terminated in the D programming language?

2022-04-06 Thread Stanislav Blinov via Digitalmars-d-learn
or not. Please provide any relevant examples of how you do this. In general, you shouldn't do that. In D, a `string`, `wstring` and `dstring` are slices of corresponding character types, and are *not* null-terminated (and in fact can contain 0 within their representation). However, as Andrea

Re: How to print or check if a string is "\0" (null) terminated in the D programming language?

2022-04-06 Thread Andrea Fontana via Digitalmars-d-learn
On Wednesday, 6 April 2022 at 08:55:43 UTC, BoQsc wrote: I have a feeling that some parts of my code contains unterminated strings and they do overflow into other string that is to be combined. I'd like to take a look at strings, analyse them manually and see if any of them end up terminated

How to print or check if a string is "\0" (null) terminated in the D programming language?

2022-04-06 Thread BoQsc via Digitalmars-d-learn
I have a feeling that some parts of my code contains unterminated strings and they do overflow into other string that is to be combined. I'd like to take a look at strings, analyse them manually and see if any of them end up terminated or not. Please provide any relevant examples of how you

Re: Null pointer in __vptr

2021-11-22 Thread frame via Digitalmars-d-learn
On Monday, 22 November 2021 at 13:21:22 UTC, bauss wrote: Seems like it was only partially fixed then, specifically for missing abstract methods, but not whether the signature was correct or not. Seems like a critical bug to me. At least it's still present in dmd 2.098.

Re: Null pointer in __vptr

2021-11-22 Thread bauss via Digitalmars-d-learn
the null method made its way to the binary. You got it! It was an abstract class involved and the method signature was just wrong, but no compile error. Thanks! Seems like it was only partially fixed then, specifically for missing abstract methods, but not whether the signature was correct

Re: Null pointer in __vptr

2021-11-21 Thread frame via Digitalmars-d-learn
On Sunday, 21 November 2021 at 02:43:12 UTC, Ali Çehreli wrote: There are some offset arithmetic involved to get to the right vtbl pointer but the pointer of an object cannot be stored anywhere in the vtbl because there is just one vtbl but very many objects. Ok, so they mean by instance is

Re: Null pointer in __vptr

2021-11-20 Thread Ali Çehreli via Digitalmars-d-learn
On 11/20/21 7:19 AM, frame wrote: > I think this is true for an object instance. But from an interface > instance, the object instance must be accessible somewhere? Everything needed is available from the pointer to a class object (or interface). There are some offset arithmetic involved to

Re: Null pointer in __vptr

2021-11-20 Thread frame via Digitalmars-d-learn
On Friday, 19 November 2021 at 21:09:16 UTC, Ali Çehreli wrote: I am not sure that's correct. The way I picture it, the code reaches the __vptr by following a pointer; so it's already known. Additionally, I am under the impression that there is only one __vptr for a given type, which all

Re: Null pointer in __vptr

2021-11-19 Thread Ali Çehreli via Digitalmars-d-learn
On 11/19/21 10:04 AM, frame wrote: > On Friday, 19 November 2021 at 15:46:41 UTC, Adam D Ruppe wrote: > >> The `destroy` function (as well as other class destruction) will null >> out the whole vtable to help make use-after-free an obvious error. >> Possible that

Re: Null pointer in __vptr

2021-11-19 Thread frame via Digitalmars-d-learn
On Friday, 19 November 2021 at 18:14:03 UTC, Adam D Ruppe wrote: I've gotten that before as a result of a compiler bug... I had an abstract method that wasn't implemented but the compile time error got swallowed by a bug and thus the null method made its way to the binary. You got

Re: Null pointer in __vptr

2021-11-19 Thread Adam D Ruppe via Digitalmars-d-learn
and thus the null method made its way to the binary. I think that bug was fixed but still you might want to check your code for the `abstract` keyword and ensure they are legit implemented in your cases.

Re: Null pointer in __vptr

2021-11-19 Thread frame via Digitalmars-d-learn
On Friday, 19 November 2021 at 15:46:41 UTC, Adam D Ruppe wrote: The `destroy` function (as well as other class destruction) will null out the whole vtable to help make use-after-free an obvious error. Possible that happened to you. So, a partial nulled table shouldn't exist, right? like

Re: Null pointer in __vptr

2021-11-19 Thread Adam D Ruppe via Digitalmars-d-learn
On Friday, 19 November 2021 at 15:37:59 UTC, frame wrote: Is a null pointer entry in the __vptr[] valid or always a sign for corruption/wrong cast somewhere? thx The `destroy` function (as well as other class destruction) will null out the whole vtable to help make use-after-free an obvious

Null pointer in __vptr

2021-11-19 Thread frame via Digitalmars-d-learn
Got a suspicious interface instance in the debugger and question myself: Is a null pointer entry in the __vptr[] valid or always a sign for corruption/wrong cast somewhere? thx

Re: How to check if value is null, today?

2021-10-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/15/21 6:39 AM, tastyminerals wrote: On Thursday, 14 October 2021 at 12:43:36 UTC, jfondren wrote: Do you have a complete example? Because this runs without error: Steven Schveighoffer was correct, the error was caused by non explicit `someVar;` which had to be changed to

Re: How to check if value is null, today?

2021-10-15 Thread tastyminerals via Digitalmars-d-learn
) writeln(a.get); if (b.isNull) writeln("b is null"); } ``` Steven Schveighoffer was correct, the error was caused by non explicit `someVar;` which had to be changed to `someVar.get;`.

Re: How to check if value is null, today?

2021-10-14 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/14/21 7:58 AM, tastyminerals wrote: The new `DMD v2.097.2` deprecated implicit null conversions `std.typecons.Nullable!double.Nullable.get_`. The deprecation warning tell you to `Please use .get explicitly.`. Here is an example code that doesn't work with the new compiler anymore

Re: How to check if value is null, today?

2021-10-14 Thread jfondren via Digitalmars-d-learn
uot;b is null"); } ```

How to check if value is null, today?

2021-10-14 Thread tastyminerals via Digitalmars-d-learn
The new `DMD v2.097.2` deprecated implicit null conversions `std.typecons.Nullable!double.Nullable.get_`. The deprecation warning tell you to `Please use .get explicitly.`. Here is an example code that doesn't work with the new compiler anymore: ``` if (someValue.isNull) ``` Attempting

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

  1   2   3   4   5   6   7   8   9   >