Author Topic: O2 quirks  (Read 7125 times)

0 Members and 1 Guest are viewing this topic.

  • Guest
O2 quirks
« on: October 14, 2018, 08:44:37 PM »
Code: [Select]
#unique on
enum
   x = 1
   y
end enum

If you use enum without an identifier, and the #unique on directive, the reported error message is that "sys" is already defined.

Without #unique on, it says that "x" is not defined.

Also, why enum requires an identifier if later isn't used for anything?

I can't do

Code: [Select]
enum x
   a = 10
   b
   c
end enum

enum x2
   a = 1
   b
   c
end enum

print x.a

They should work like namespaces if an identifier, e.g. "explicit", is specified, e.g.

Code: [Select]
enum x explicit
   a = 10
   b
   c
end enum

enum x2 explicit
   a = 1
   b
   c
end enum

print x.a
print x2.a

Currently, they're working as if I was using an anonymous enum.
« Last Edit: October 14, 2018, 09:50:45 PM by José Roca »

  • Guest
Re: O2 quirks
« Reply #1 on: October 14, 2018, 08:49:47 PM »
Code: [Select]
namespace aa
   type t
      int x
      int y
   end type
end namespace

aa::t.x = 11
print aa::t.x

"print aa::t.x" throws and "unexpected end of program" error.

I know that i must use

Code: [Select]
namespace aa
   type t
      int x
      int y
   end type
end namespace

dim tt as aa::t
tt.x = 11
print tt.x

but why aa::t.x = 11 compiles?

Looks like O2 needs much improvement regarding error checking.
« Last Edit: October 14, 2018, 09:35:53 PM by José Roca »

  • Guest
Re: O2 quirks
« Reply #2 on: October 14, 2018, 09:39:41 PM »
In other languages, the using directive allows all the names in a namespace to be used without the namespace-name as an explicit qualifier. In O2 seems to be yet another alias for "use" and "uses".

Other languages also allow an alias for the identifier of a namespace to avoid conflicts with other libraries using the same namespace identifier, e.g.

namespace foo alias "MyFoo"
« Last Edit: October 15, 2018, 05:21:42 AM by José Roca »

José Roca

  • Guest
Re: O2 quirks
« Reply #3 on: October 15, 2018, 01:49:54 AM »
Nested namespaces.

Code: [Select]
namespace aa
   int i = 10
   int x = 22
   print bb::i   ' prints 10 instead of 11, or give an error
   namespace bb
      int i = 11
      int x = 33
   end namespace
end namespace

  • Guest
Re: O2 quirks
« Reply #4 on: October 15, 2018, 05:24:03 AM »
Does :: have more uses besides working with namespaces?

In C++ can be used to access variables outside a namespace from inside a namespace, e.g.

Code: [Select]
int i = 12345

namespace aa
   int i = 10
   int x = 22
   namespace bb
      int i = 11
      int x = 33
   end namespace
   print ::i   ' access the global i variable
end namespace

In O2 it compiles, but does not work.

How can we access the global variable i from inside a namespace?
« Last Edit: October 15, 2018, 05:37:43 AM by José Roca »

Charles Pegge

  • Guest
Re: O2 quirks (Assigning to non-variables)
« Reply #5 on: October 15, 2018, 05:38:11 AM »
Attempts to assign to  a type will now be trapped!

Code: [Select]
   type t
      int x
      int y
   end type
   t.x=9 'trapped

José Roca

  • Guest
Re: O2 quirks
« Reply #6 on: October 15, 2018, 05:44:21 AM »
Funny, this works!

Code: [Select]
int i = 12345

namespace aa
   int i = 10
   int x = 22
   namespace bb
      int i = 11
      int x = 33
   end namespace
   print std::i   ' access the global i variable
end namespace

Looks like I have to learn C++ to understand O2 :)

So the global namespace is called "std"...

Charles Pegge

  • Guest
Re: O2 quirks (nested namespaces)
« Reply #7 on: October 15, 2018, 05:47:18 AM »
I could make namespaces nestable.

If you used nested scopes instead, would this be a viable alternative?

  • Guest
Re: O2 quirks
« Reply #8 on: October 15, 2018, 06:00:07 AM »
Anything that you consider that can be useful. I'm just trying to learn the language.

I looked at namespaces and found a line of code that uses the "::" operator.

Then I tried to use ::i to access the global i variable, but did not work.

Then I looked at the C++ documentation for "::" and I saw that they also used std::, so I tried std::i and it works.

Above all, error trapping is fundamental to me.

Charles Pegge

  • Guest
Re: O2 quirks (accessing global namespace from within)
« Reply #9 on: October 15, 2018, 06:06:52 AM »

This works by accident :)

Code: [Select]
namespace aa
   int i = 10
   int x = 22
   print std::i   ' access the global i variable
end namespace

It could be global::i



  • Guest
Re: O2 quirks
« Reply #10 on: October 15, 2018, 06:10:55 AM »
No problem: I will use "global".

Discovered by mistake:

Code: [Select]
print print "ok"

First prints "ok" and then a number.

This also compiles:

Code: [Select]
print print print print "ok"

Charles Pegge

  • Guest
Re: O2 quirks (print print print ...)
« Reply #11 on: October 15, 2018, 06:20:23 AM »

This is legit, strange as it may seem.

Code: [Select]
print print print print "ok"

The compiler sees it like this:

Code: [Select]
print print( print ( print( "ok") ) )

José Roca

  • Guest
Re: O2 quirks
« Reply #12 on: October 15, 2018, 06:42:33 AM »
But this does not work.

Code: [Select]
int i = 12345

namespace aa
   int i = 10
   int x = 22
   print global::i
end namespace

It prints 10 instead of 12345.

José Roca

  • Guest
Re: O2 quirks (print print print ...)
« Reply #13 on: October 15, 2018, 06:50:10 AM »

This is legit, strange as it may seem.

Code: [Select]
print print print print "ok"

The compiler sees it like this:

Code: [Select]
print print( print ( print( "ok") ) )

Do you mean that print is a function instead of an statement?

Charles Pegge

  • Guest
Re: O2 quirks (print is a procedure)
« Reply #14 on: October 15, 2018, 07:49:10 AM »
Yes, print is an overrideable core procedure, not a statement.