Jump to page: 1 29  
Page
Thread overview
Adding the ?. null verification
Jun 18, 2014
Etienne
Jun 18, 2014
Kapps
Jun 18, 2014
Etienne
Jun 19, 2014
Jacob Carlborg
Jun 19, 2014
Craig Dillabaugh
Jun 20, 2014
Jacob Carlborg
Jun 18, 2014
bearophile
Jun 18, 2014
Etienne
Jun 20, 2014
Bienlein
Jun 18, 2014
Lionello Lunesu
Jun 18, 2014
Etienne
Jun 18, 2014
Ary Borenszweig
Jun 18, 2014
Etienne
Jun 19, 2014
Lionello Lunesu
Jun 18, 2014
Frustrated
Jun 18, 2014
deadalnix
Jun 18, 2014
Etienne
Jun 18, 2014
deadalnix
Jun 19, 2014
Meta
Jun 18, 2014
Mattcoder
Jun 18, 2014
Marc Schütz
Jun 18, 2014
Mattcoder
Jun 18, 2014
H. S. Teoh
Jun 18, 2014
Etienne
Jun 18, 2014
H. S. Teoh
Jun 18, 2014
bearophile
Jun 18, 2014
Dmitry Olshansky
Jun 18, 2014
Meta
Jun 19, 2014
Timon Gehr
Jun 19, 2014
Etienne
Jun 19, 2014
bearophile
Jun 19, 2014
H. S. Teoh
Jun 19, 2014
Yota
Jun 19, 2014
Tofu Ninja
Jun 19, 2014
H. S. Teoh
Jun 19, 2014
Tobias Pankrath
Jun 19, 2014
Etienne
Jun 19, 2014
Timon Gehr
Jun 20, 2014
Jacob Carlborg
Jun 19, 2014
H. S. Teoh
Jun 19, 2014
logicchains
Jun 19, 2014
Nordlöw
Jun 18, 2014
Adam D. Ruppe
Jun 19, 2014
Nick Treleaven
Jun 19, 2014
Etienne
Jun 19, 2014
H. S. Teoh
Jun 19, 2014
Etienne
Jun 19, 2014
Nick Treleaven
Jun 19, 2014
Tofu Ninja
Jun 19, 2014
H. S. Teoh
Jun 19, 2014
Etienne
Jun 19, 2014
H. S. Teoh
Jun 19, 2014
Etienne
Jun 19, 2014
Etienne
Jun 19, 2014
H. S. Teoh
Jun 19, 2014
H. S. Teoh
Jun 20, 2014
Etienne
Jun 20, 2014
H. S. Teoh
Jun 20, 2014
Etienne
Jun 19, 2014
H. S. Teoh
Jun 20, 2014
Etienne
Jun 19, 2014
H. S. Teoh
Jun 21, 2014
H. S. Teoh
Jun 21, 2014
deadalnix
Jun 23, 2014
David Gileadi
Jun 22, 2014
H. S. Teoh
Jun 24, 2014
Yota
Jun 24, 2014
Jacob Carlborg
Jun 24, 2014
H. S. Teoh
Jun 24, 2014
Meta
Oct 21, 2014
Meta
Oct 22, 2014
H. S. Teoh
Jun 24, 2014
Ary Borenszweig
Jun 25, 2014
Jacob Carlborg
Jun 19, 2014
Vladimir Panteleev
June 18, 2014
I find myself often repeating this boilerplate:

if (obj.member !is null)
{
	if (obj.member.nested !is null)
	{
		if (obj.member.nested.val !is null)
		{
			writeln(obj.member.nested.val);
		}
	}
}

I have to admit it's a little frustrating when you see swift's ?. syntax notation, it would be a little more practical to be able to write

writeln(obj.member?.nested?.val);


Based on http://appventure.me/2014/06/13/swift-optionals-made-simple/


Any thoughts?

June 18, 2014
On Wednesday, 18 June 2014 at 15:42:04 UTC, Etienne wrote:
> I find myself often repeating this boilerplate:
>
> if (obj.member !is null)
> {
> 	if (obj.member.nested !is null)
> 	{
> 		if (obj.member.nested.val !is null)
> 		{
> 			writeln(obj.member.nested.val);
> 		}
> 	}
> }
>
> I have to admit it's a little frustrating when you see swift's ?. syntax notation, it would be a little more practical to be able to write
>
> writeln(obj.member?.nested?.val);
>
>
> Based on http://appventure.me/2014/06/13/swift-optionals-made-simple/
>
>
> Any thoughts?

C# is getting the same syntax, and I remember there being some discussion about it here. It's somewhat useful I suppose, though I think it's made significantly more useful in C# with 'a ?? b' (a if a is not null, else b).
June 18, 2014
> C# is getting the same syntax, and I remember there being some
> discussion about it here. It's somewhat useful I suppose, though I think
> it's made significantly more useful in C# with 'a ?? b' (a if a is not
> null, else b).


I've seen too many bugs where you end up dereferencing a null pointer, also with the -> being replaced by . in D this is even more confusing when you switch a member from a pointer to a struct. The compiler could easily advise you to use the .? notation when you're moving in too deep into no man's land.
June 18, 2014
Etienne:

> writeln(obj.member?.nested?.val);

What about an approach like Scala instead?

Bye,
bearophile
June 18, 2014
On 2014-06-18 11:55 AM, bearophile wrote:
> Etienne:
>
>> writeln(obj.member?.nested?.val);
>
> What about an approach like Scala instead?
>
> Bye,
> bearophile

You mean like this?


http://stackoverflow.com/questions/1163393/best-scala-imitation-of-groovys-safe-dereference-operator

def ?[A](block: => A) =
  try { block } catch {
    case e: NullPointerException if e.getStackTrace()(2).getMethodName == "$qmark" => null
    case e => throw e
  }

val a = ?(b.c.d.e)

June 18, 2014
On 18/06/14 23:42, Etienne wrote:
> I find myself often repeating this boilerplate:
>
> if (obj.member !is null)
> {
>      if (obj.member.nested !is null)
>      {
>          if (obj.member.nested.val !is null)
>          {
>              writeln(obj.member.nested.val);
>          }
>      }
> }
>
> I have to admit it's a little frustrating when you see swift's ?. syntax
> notation, it would be a little more practical to be able to write
>
> writeln(obj.member?.nested?.val);
>
>
> Based on http://appventure.me/2014/06/13/swift-optionals-made-simple/
>
>
> Any thoughts?

I want non-null in the type system! To put a nullable reference into a non-null variable you need to check first, but from then on the compiler can ensure it's never null!
June 18, 2014
> I want non-null in the type system! To put a nullable reference into a
> non-null variable you need to check first, but from then on the compiler
> can ensure it's never null!

That would be great but useless for my situation involving some complex AST's

class AEntity
{
	Module head;
	string ident;
	Type type;
	AEntity typeInfo;
	AEntity[] members; // for definitions
	AEntity[] parameters; // if parameterized
...
}

It's very useful to descend through parts of a complex tree without adding plenty of bool checks

foreach (AEntity member; entity.members)
{
	if (member.typeInfo !is null)
	{
		AEntity curr = member;
		member.ident ~= member.typeInfo.ident;
		curr = member.typeInfo;
		while (curr.typeInfo !is null)
		{
			member.ident ~= "." ~ curr.typeInfo.ident;
			curr = curr.typeInfo;
		}
	}
}
June 18, 2014
On 6/18/14, 1:17 PM, Etienne wrote:
>> I want non-null in the type system! To put a nullable reference into a
>> non-null variable you need to check first, but from then on the compiler
>> can ensure it's never null!
>
> That would be great but useless for my situation involving some complex
> AST's
>
> class AEntity
> {
>      Module head;
>      string ident;
>      Type type;
>      AEntity typeInfo;
>      AEntity[] members; // for definitions
>      AEntity[] parameters; // if parameterized
> ...
> }
>
> It's very useful to descend through parts of a complex tree without
> adding plenty of bool checks
>
> foreach (AEntity member; entity.members)
> {
>      if (member.typeInfo !is null)
>      {
>          AEntity curr = member;
>          member.ident ~= member.typeInfo.ident;
>          curr = member.typeInfo;
>          while (curr.typeInfo !is null)
>          {
>              member.ident ~= "." ~ curr.typeInfo.ident;
>              curr = curr.typeInfo;
>          }
>      }
> }

But if you don't add the bool checks you might get segmentation fault. Or am I missing something?

June 18, 2014
> But if you don't add the bool checks you might get segmentation fault.
> Or am I missing something?
>

In D all pointers are zero-filled on initialization so it counts as a bool for me and null is the same as if you had a *.init
June 18, 2014
On Wednesday, 18 June 2014 at 15:42:04 UTC, Etienne wrote:
> I find myself often repeating this boilerplate:
>
> if (obj.member !is null)
> {
> 	if (obj.member.nested !is null)
> 	{
> 		if (obj.member.nested.val !is null)
> 		{
> 			writeln(obj.member.nested.val);
> 		}
> 	}
> }
>
> I have to admit it's a little frustrating when you see swift's ?. syntax notation, it would be a little more practical to be able to write
>
> writeln(obj.member?.nested?.val);
>
>
> Based on http://appventure.me/2014/06/13/swift-optionals-made-simple/
>
>
> Any thoughts?

Would be great! I think though, one could simplify it, you don't need multiple ?'s(one would do)

if (?obj.member.nested.val == null)
{
...
}

?object returns null if the object is null, even if it is dereferenced.

possibly a better syntax would be

if (??obj.member.nested.val == null)

If used as a rval one could have

auto x = ??obj.member.nested.val;

x is null if obj, obj.member, or obj.member.nested is null. Else obj.member.nested.val.

I'm just curious who is going to add the the code to D?

If you don't like ?? (confusing it with the null-coalescing operator) then what about ?*, *?, ?&, &?, ???, etc...

« First   ‹ Prev
1 2 3 4 5 6 7 8 9