Forums

conditional/ternary, coalesce

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

conditional/ternary, coalesce

Postby nevdelap » Mon Jul 05, 2010 8:23 pm

Hi Chuck, more questions.

Why did you chose to diverge from the well known "?:" and "??" operators in favor of if(,,) and ? to do the same things?

if(,,) seems un-Cobra to me with it's brackets, and changing the meaning of ? from what people are used to seeing in other languages...

Just wondering what the thought process was.

Nev
call me Nev.
nevdelap
 
Posts: 61
Location: Buenos Aires

Re: conditional/ternary, coalesce

Postby Charles » Tue Jul 06, 2010 9:09 pm

Keep in mind that Cobra's syntax derives mostly from Python and not from C/C++/C# which is where ?: comes from.

Also, Cobra often kicks things off with a keyword. This even includes many expressions such as "for", "ref", "any" and "all".

Most importantly, from a language design perspective, if a type of expression has an equivalent or analogous statement, I prefer the same keyword: for-statement and for-expression; if-statement and if-expression. If we ever added a conditional looping expression, you can predict it would start with "while".

I did debate on "if(cond, tpart, fpart)" vs. "if cond get tpart else fpart". Note that "get" is already a keyword used for a similar purpose in for-expressions and, of course, "else" is a keyword already used in if-statements. Now that I have used "if(cond, tpart, fpart)", I find that the commas don't provide separation as good as I would like to see. On the plus side "if(,,)" is tighter/shorter than "if get else".

Regarding ??, Cobra has the same thing with ?, but it doesn't have to be doubled since there is no ternary ?: operator that has to be distinguished from.

So any opinions on if(,,) vs. if-get-else?
code = if(.isSigned, -1, 1) * .size
code = (if .isSigned get -1 else 1) * .size # or,
code = .size * if .isSigned get -1 else 1

_superNode = if(.compiler.nodeStack.count > 0, .compiler.nodeStack.peek, _superNode ? nil)
_superNode = if .compiler.nodeStack.count > 0 get .compiler.nodeStack.peek else _superNode ? nil

return if(token.which == 'OPEN_DO', .paramDecls(true), List<of Param>())
return if token.which == 'OPEN_DO' get .paramDecls(true) else List<of Param>()

The if-get-else form is probably done some injustice by the lack of syntax highlighting which would provide better separation:

return if token.which == 'OPEN_DO' get .paramDecls(true) else List<of Param>()

See also for-expression.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: conditional/ternary, coalesce

Postby nevdelap » Wed Jul 07, 2010 2:38 pm

My vote would be for if get else over if(,,).

The bracketed syntax just looks weird. It doesn't look Cobra-y or Python-y. I can't put my finger on it exactly, but maybe it looks too much like a function or method call despite being 'if'.

On the plus side "if(,,)" is tighter/shorter than "if get else".


If you were aiming at tightness and shortness ?: would be the tightest and shortest, so I don't think it's actually a real plus for if(,,) - rather an argument for ?: instead of if(,,).

And my 2 cents worth on ? rather than ?? - the reason of simply not having to differentiate from a ? in Cobra doesn't seem to me like enough reason to be different from C#'s operator in that it makes a point of confusion that doesn't need to be.

Thanks.
call me Nev.
nevdelap
 
Posts: 61
Location: Buenos Aires

Re: conditional/ternary, coalesce

Postby nevdelap » Wed Jul 07, 2010 3:03 pm

I put your code in gedit, and yeah, the if get else stands out nicely.

Image

It doesn't help that gedit currently wants to highlight if( as a function call and doesn't highlight the ? as an operator inside it, but even if it did them right it would still look less clear with the commas, like you said.

?? stands out clearly as a different thing from question marks marking nilable types, Yes, sure, they are butted up against their type and used in completely different contexts, but still, I don't think it hurts.

Image
call me Nev.
nevdelap
 
Posts: 61
Location: Buenos Aires

Re: conditional/ternary, coalesce

Postby hopscc » Thu Jul 08, 2010 5:45 am

Mines (now) for the status quo ( if(,,) rather than if/get/else ( why isnt it if/get/elseget ? )

Initially I would have preferred something like (?:) ( with the enclosing '()' for explicit expression disambiguation) just from historical familiarity
but the rest of cobra is minimalist and keyword based and so should this be and this works for me...

I note that in any use of if/get/else thats embedded in an expression youd usually ( like ?: in C) wrap it in () to
clarify the entire construct for readers - thats not necessary with if(,,) its already visually contained.

It seems to me that the items you're complaining about with if(,,) are artifacts of your editors
ie make gedit hilite 'if(' properly, differently from 'if' and choose a better font and size than the example used in the image so that commas/punctuation is visible


Capture-4.jpg
Capture-4.jpg (67.27 KiB) Viewed 8187 times


I quite like the ? as a null-coalesce ( vs ?? or anything else) it fits nicely with trailing ? on types to indicate ability to be null....
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: conditional/ternary, coalesce

Postby nevdelap » Thu Jul 08, 2010 3:39 pm

I don't agree with the artifacts of the editor comment. Yes, if if the if( was the right color it would be better, and I pointed that out, but using a big font to make punctuation stand out wouldn't make me like Perl any better, so using a bigger font just to see commas in Cobra doesn't make an argument to me.
call me Nev.
nevdelap
 
Posts: 61
Location: Buenos Aires

Re: conditional/ternary, coalesce

Postby nevdelap » Thu Jul 08, 2010 5:17 pm

Thinking out loud...

These exist...
if b
a = c
else
a = d

if b, a = c
else, a = d

Therefore...?
a = if b, c else d

Seems to fit the criteria for keyword based, same order as if statement, and clean, and nicely Cobra-ish, though still has one comma it doesn't mandate the ugly brackets that look like a function.

Opinions?
call me Nev.
nevdelap
 
Posts: 61
Location: Buenos Aires

Re: conditional/ternary, coalesce

Postby _Gold_Fish » Thu Jul 08, 2010 6:40 pm

Alright, my two cents on this:

I don't like the if(,,) layout, it doesn't look like cobra to me. I like the if-get-else idea, but I think perhaps it should be if-get-else-get instead. And, perhaps separate the elements with commas? I know that it isn't as compact, but how much longer would it really take to type two commas?

Also, would it be possible to instead it be instead:
get if <logic>, <something> else [if] <something>,

I think having the "get" keyword infront of the "if" would make more sense. (maybe this could be expanded to work with "branch" as well?)

Again, just my $0.02.
_Gold_Fish
 
Posts: 18

Re: conditional/ternary, coalesce

Postby hopscc » Fri Jul 09, 2010 7:03 am

Agreed - The
Code: Select all
a = if b, c else d

expression is succinct and arguably more cobra -ish ( whatever you take that to mean).

though embedded in an expression you'd still want it bracketed
Code: Select all
a = (if b, c else d) * f + g

code = (if .isSigned, -1 else 1) * .size
code = .size * (if .isSigned, -1 else 1)

_superNode = (if .compiler.nodeStack.count > 0, .compiler.nodeStack.peek else _superNode) ? nil  # slightly tweaked from previous

return if token.which == 'OPEN_DO', .paramDecls(true) else List<of Param>()   
return (if token.which == 'OPEN_DO', .paramDecls(true) else List<of Param>())  # better ??


So I wouldnt say its that much a win over the incumbent ( if(,,) ), merely different.

and
nevdelap - I want to emphasize that I said to use a better font not (necessarily) a bigger one as you quoted (twice).
(better here being a font that works better personally to easily distinguish all the tokens ( snidely here I might suggest a custom one that displays commas as a one glyph 'else' :) )

The example I gave is better (for me at least) in that it I can clearly see and differentiate text from whitespace and easily identify punctuation separate from both and each other
(incl commas) . Incidentally to that its also larger :) . YMMV
I used the same editor/font when I did perl programming and it unfortunately didnt make me like perl any better either (though I could easily distinguish all the tokens).
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand


Return to Discussion

Who is online

Users browsing this forum: No registered users and 82 guests

cron