June 20, 2014 Re: Adding the ?. null verification | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On 2014-06-19 6:30 PM, H. S. Teoh via Digitalmars-d wrote:
> On Thu, Jun 19, 2014 at 03:23:33PM -0700, H. S. Teoh via Digitalmars-d wrote:
> [...]
>> Unfortunately, it appears that opDispatch has become too complex to be
>> inlined, so now gdc is unable to simplify it to a series of nested
>> if's. :-(
> [...]
>
> Surprisingly, if we just stick .exists in there unconditionally, like
> you did, then gdc actually optimizes it away completely, so that we're
> back to the equivalent of nested if's! So your solution is superior
> after all. :)
>
>
> T
>
Oh I just saw this. Good, so I can keep my .or() method ! :)
|
June 20, 2014 Re: Adding the ?. null verification | ||||
---|---|---|---|---|
| ||||
Posted in reply to Etienne | On Wednesday, 18 June 2014 at 15:57:40 UTC, Etienne wrote:
> 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)
I think he means to use the Option class instead of returning null. Also Rust does it that way.
|
June 20, 2014 Re: Adding the ?. null verification | ||||
---|---|---|---|---|
| ||||
Posted in reply to Etienne | On Fri, Jun 20, 2014 at 08:57:46AM -0400, Etienne via Digitalmars-d wrote: > On 2014-06-19 6:23 PM, H. S. Teoh via Digitalmars-d wrote: > > > >Unfortunately, it appears that opDispatch has become too complex to > >be inlined, so now gdc is unable to simplify it to a series of nested > >if's. > >:-( > > > > > >T > > > > Meh, I don't mind specifying that condition manually after all... having a default value isn't really on top of my list =) True. Actually, I did my disassembly test again, and now I can't seem to coax gdc to optimize out the .exists flag, esp. when .or is involved. Perhaps that was a little too ambitious; maybe it's better to stick with the original simple solution after all. :P T -- Laissez-faire is a French term commonly interpreted by Conservatives to mean 'lazy fairy,' which is the belief that if governments are lazy enough, the Good Fairy will come down from heaven and do all their work for them. |
June 20, 2014 Re: Adding the ?. null verification | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On 2014-06-20 10:29 AM, H. S. Teoh via Digitalmars-d wrote:
> True. Actually, I did my disassembly test again, and now I can't seem to
> coax gdc to optimize out the .exists flag, esp. when .or is involved.
> Perhaps that was a little too ambitious; maybe it's better to stick with
> the original simple solution after all. :P
>
>
> T
>
Try marking the or method as const, and the bool as immutable maybe?
|
June 20, 2014 Re: Adding the ?. null verification | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | On 2014-06-19 16:37, Craig Dillabaugh wrote: > Is this any better than? > > if(!a) a = b; I would say it's about the same as "a ?? b" is better than "a ? a : b". It's get better since you can use it directly in a return statement: void a () { return a ??= new Object; } -- /Jacob Carlborg |
June 20, 2014 Re: Adding the ?. null verification | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On 2014-06-18 21:36, H. S. Teoh via Digitalmars-d wrote: > Here's a first stab at a library solution: I thought of adding a field to indicate if a value if present or not. If the value is accessed when it's not present it would assert/throw. -- /Jacob Carlborg |
June 21, 2014 Re: Adding the ?. null verification | ||||
---|---|---|---|---|
| ||||
Posted in reply to Etienne | On 6/19/14, 1:29 PM, Etienne wrote:
> writeln(currAssignment.safeDeref.typeInfo.ident.or("meh"));
"or" is really nice and terse. I think we should add that to std. safeDeref isn't the best choice of name.
Andrei
|
June 21, 2014 Re: Adding the ?. null verification | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Sat, Jun 21, 2014 at 03:26:45PM -0700, Andrei Alexandrescu via Digitalmars-d wrote: > On 6/19/14, 1:29 PM, Etienne wrote: > >writeln(currAssignment.safeDeref.typeInfo.ident.or("meh")); > > "or" is really nice and terse. I think we should add that to std. safeDeref isn't the best choice of name. [...] What's your suggestion? T -- Once the bikeshed is up for painting, the rainbow won't suffice. -- Andrei Alexandrescu |
June 21, 2014 Re: Adding the ?. null verification | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Saturday, 21 June 2014 at 22:40:32 UTC, H. S. Teoh via Digitalmars-d wrote:
> On Sat, Jun 21, 2014 at 03:26:45PM -0700, Andrei Alexandrescu via Digitalmars-d wrote:
>> On 6/19/14, 1:29 PM, Etienne wrote:
>> >writeln(currAssignment.safeDeref.typeInfo.ident.or("meh"));
>>
>> "or" is really nice and terse. I think we should add that to std.
>> safeDeref isn't the best choice of name.
> [...]
>
> What's your suggestion?
>
>
> T
maybe is fine. Last time I checked, it isn't only a monad in haskell, but also vastly used by these peoples who speak english.
|
June 22, 2014 Re: Adding the ?. null verification | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On 6/21/14, 3:38 PM, H. S. Teoh via Digitalmars-d wrote:
> On Sat, Jun 21, 2014 at 03:26:45PM -0700, Andrei Alexandrescu via Digitalmars-d wrote:
>> On 6/19/14, 1:29 PM, Etienne wrote:
>>> writeln(currAssignment.safeDeref.typeInfo.ident.or("meh"));
>>
>> "or" is really nice and terse. I think we should add that to std.
>> safeDeref isn't the best choice of name.
> [...]
>
> What's your suggestion?
That we add "or" to std and find a name for safeDeref that doesn't use the word "safe" in a confusing manner. -- Andrei
|
Copyright © 1999-2021 by the D Language Foundation