Forums

questions

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

questions

Postby natter » Sun Feb 15, 2009 5:50 am

1:
dont you think it would be better to write
Code: Select all
x = y as int
# instead of
x = y to int

i mean the variable doesnt change its type, just the value
'string as int' sounds valuerelated
'string to int' sounds variablerelated
also 'as' is already used for declaring a type and would naturally fit in
'to' would be free as keyword 'for 1 to 5' instead of 'for 1 .. 5' what looks bad btw

2:
i like the idea of 'post while' but i dont like that it is two words
did you thought about a single word like 'until'?

3:
often you code something like
Code: Select all
x = foo()
if x > -1
  bar()

did you thought about a way to bring the first two lines into one?

4:
what do you think about single line if
Code: Select all
.quit if .keyHit
# instead of
if .keyHit, .quit
natter
 
Posts: 20

Re: questions

Postby Charles » Sun Feb 15, 2009 10:49 am

1:
# you can write a var decl like so:
x as int

# and you can init it:
x as int = y

# if y is an int, you can rely on type inference
x = y

# if you need to force the int type (perhaps because y is dynamic,
# but you know the type at this point) you can do:
x as int = y
# or:
x = y to int

I personally like the last form because it maintains the simple left hand side ("x = ...") that characterizes most other assignments. Feel free to use whichever you prefer.

2: I didn't go with "until" because it negates the condition. "post while" uses the same boolean condition as "while" but just moves the check down.

3: In your example you can write:
if foo() > -1
bar()
# if you really meant in a case where the value of foo() will be reused, you can do this in two lines:
x = foo()
if x > -1, bar(x)

I didn't allow assignment in the conditional because it is a common source of errors, even for experienced developers.

4: I chose consistency over flexibility in this area. The "if" always comes first.

I don't currently have any syntax changes planned in these areas.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: questions

Postby jonathandavid » Sun Feb 15, 2009 11:46 pm

Yes, I can read, and yes, I've read your last sentence. But yet I felt compelled to say this: I, like the OP, find the "post while" construct a little counter-intuitive. What made you shy away from the obvious:

repeat
i = i + 1;
while i < 100


This is much more readable in my IMHO... If you don't want to define a new keyword, you can use "do" instead of "repeat", as in the C family of languages. I'm sure there is a reason why you consider "post while" superior, I just wanted to know it.
jonathandavid
 
Posts: 159

Re: questions

Postby Csaba » Mon Feb 16, 2009 1:40 am

Chuck,
Maybe too late to influence loop construct in Cobra, but I always prefer the general loop construct with possibility to have a break statement or a continue the loop or both:
Code: Select all
loop
      …
      if <test> break
      …
      if <test> continue
      …

(This general "loop" is something Wirth missed in Pascal). Usually some “anormalities” are "needed" to make more compact code. To replace the “while” loop constructs, it is :
Code: Select all
loop if <test> break  #  you may have the test on the same line without statement separator
      …

EDIT, removed an erroneous suggestion here)
The "if" test has the same logic as an "until" test, but this logic is straightforward to interpret for a human.
Since Cobra, due to its design, don’t have any “end loop” there is no need for an anormality at the end of the loop. It should be just:
Code: Select all
loop
   …
   if <test> break  # test of condition at the loop end – replaces Pascal repeat construct

I find the “post while” construct counter intuitive, even if I understand how you got to the conclusion.
Csaba
 
Posts: 42

Re: questions

Postby Charles » Tue Feb 17, 2009 4:17 pm

jonathandavid wrote:Yes, I can read, and yes, I've read your last sentence. But yet I felt compelled to say this: I, like the OP, find the "post while" construct a little counter-intuitive. What made you shy away from the obvious:

repeat
i = i + 1;
while i < 100


This is much more readable in my IMHO... If you don't want to define a new keyword, you can use "do" instead of "repeat", as in the C family of languages. I'm sure there is a reason why you consider "post while" superior, I just wanted to know it.

I considered that approach, but rejected it because at a glance it can look like two statements: a "repeat" statement and a "while" statement. The reason it looks funny to me is the combination of (1) all other compound statements are without an ending, un-indented line and (2) there is a "while" statement.

So I actually find both approaches to be somewhat counter-intuitive. I decided "post while" was the lesser of two evils.

Btw the increment line would be:
i += 1

And no semicolons. :-)
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: questions

Postby Charles » Tue Feb 17, 2009 4:23 pm

Csaba wrote:Chuck,
Maybe too late to influence loop construct in Cobra, but I always prefer the general loop construct with possibility to have a break statement or a continue the loop or both:
Code: Select all
loop
      …
      if <test> break
      …
      if <test> continue
      …

(This general "loop" is something Wirth missed in Pascal). ...

Cobra does, in fact, have "break" and "continue" just in case someone gets the wrong impression here.

Both Pascal and Cobra offer "while true".

The idea of a "loop" keyword is interesting and one that I've played with in language designs in the past. However, after > 20 years of writing Pascal, C-family and Python code, the keywords "for" and "while" are irreversibly etched in my brain. :-)
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 44 guests