Oxygen Basic

Programming => Problems & Solutions => Topic started by: on October 14, 2018, 08:44:37 PM

Title: O2 quirks
Post by: 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.
Title: Re: O2 quirks
Post by: 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.
Title: Re: O2 quirks
Post by: 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"
Title: Re: O2 quirks
Post by: José Roca 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
Title: Re: O2 quirks
Post by: 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?
Title: Re: O2 quirks (Assigning to non-variables)
Post by: Charles Pegge 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
Title: Re: O2 quirks
Post by: José Roca 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"...
Title: Re: O2 quirks (nested namespaces)
Post by: Charles Pegge 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?
Title: Re: O2 quirks
Post by: 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.
Title: Re: O2 quirks (accessing global namespace from within)
Post by: Charles Pegge 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


Title: Re: O2 quirks
Post by: 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"
Title: Re: O2 quirks (print print print ...)
Post by: Charles Pegge 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") ) )
Title: Re: O2 quirks
Post by: José Roca 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.
Title: Re: O2 quirks (print print print ...)
Post by: José Roca 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?
Title: Re: O2 quirks (print is a procedure)
Post by: Charles Pegge on October 15, 2018, 07:49:10 AM
Yes, print is an overrideable core procedure, not a statement.
Title: Re: O2 quirks (can reopen a namespace)
Post by: Charles Pegge on October 15, 2018, 09:25:01 AM
In o2, a namespace can be reopened any time for simpler access to its variables.

But this may conflict with any implementation of nested namespaces.
Title: Re: O2 quirks
Post by: on October 15, 2018, 01:52:30 PM
I never have needed to use nested namespaces. I just have tried it to see if they are supported or not, but it is not something that I need. If they are not, I simply will put in my notes: "Nested namespaces are not supported". End of the history. What would be useful is that if they're not supported the compiler will catch any attempt to do it as an error. As you can see, I'm obsessed with error trapping.

In your example in the help file

Code: [Select]
int i=1
'
namespace aa
int i=10
namespace bb
int i=20
end namespace
'
print i + aa::i + bb::i '31
'
namespace bb
print i '20
end namespace

I noticed that you are opening namespace bb before closing namespace aa and I thought that nested namespaces were supported. But maybe you forget to close the namespace? This is another error that should be trapped :)

What really interests me are the constructors and transient objects we already have talked about. Thanks to them, and the oveloading operators, that in FB work with user defined types, I have developed several data types with FreeBasic to support variants, safe arrays, currency, decimal, etc.

> Yes, print is an overrideable core procedure, not a statement.

With overrideable do you mean that I can write my own overloaded print procedure? If yes, which is the prototype? That could be very useful, for example, to print the contents of objects like a variant.
Title: Re: O2 quirks
Post by: JRS on October 15, 2018, 03:24:38 PM
I heard the word VARIANT twice in that post. Are you getting close with a COM O2 solution?
Title: Re: O2 quirks
Post by: José Roca on October 15, 2018, 03:41:01 PM
I can't do anything until my suggested changes regarding constructors and transient objects are implemented.
Title: Re: O2 quirks (namespaces / using with)
Post by: Charles Pegge on October 16, 2018, 02:30:51 AM
Will keep namespaces non-nestable, which means that they do not have to be ended before starting another one. (this also applies to extern blocks).

namespace names will be able to contain extra double colons:

namespace ns::lengthy::deep


A simple extension of with syntax will enable lengthy namespace prefixes to be represented by 2 dots '..'

Code: [Select]
namespace ns::lengthy::deep
  int a=2
end namespace
'
with ns::lengthy::deep::
  print ns::lengthy::deep::a '2
  ..a = 4 'in assignment
  print ..a + ..a 'in expression
  int b[10]
  b[..a]=5 'syntax testing as indexer
end with
print b[4] '5
Title: Re: O2 quirks (enumerations anon and named)
Post by: Charles Pegge on October 16, 2018, 02:51:42 AM
Named and anonymous enumerations will be supported.

If unique is disabled, it will still be possible to use named-enumeration members as though they were anonymous.

Code: [Select]
#unique on
enum bit ee
 a,b,c,d
end enum

print ee.c '4

Code: [Select]
#unique off
enum bit ee
 a,b,c,d
end enum

print c '4
Title: Re: O2 quirks (core function overloading / print)
Post by: Charles Pegge on October 16, 2018, 04:28:18 AM

An example that relays back to the core print function:

Code: [Select]
function print(float a,b)
=========================
  print str(a,b) 'relay to core print function
end function

'#recordof print
print pi(),5
print "ok"

Title: Re: O2 quirks (print overloading)
Post by: Charles Pegge on October 16, 2018, 04:38:44 AM
Another example. using a class or UDT:

Code: [Select]
'print overloading

type vector
===========
  float x,y,z
end type

function print(vector*v)
========================
  print v.x ", " v.y ", " v.z 'relay to core print
end function

vector a={1.5, 2.5, 3.5}
print a
print "ok"