Oxygen Basic
Programming => Problems & Solutions => Topic started by: on October 14, 2018, 08:44:37 PM
-
#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
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.
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.
-
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
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.
-
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"
-
Nested namespaces.
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
-
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.
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?
-
Attempts to assign to a type will now be trapped!
type t
int x
int y
end type
t.x=9 'trapped
-
Funny, this works!
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"...
-
I could make namespaces nestable.
If you used nested scopes instead, would this be a viable alternative?
-
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.
-
This works by accident :)
namespace aa
int i = 10
int x = 22
print std::i ' access the global i variable
end namespace
It could be global::i
-
No problem: I will use "global".
Discovered by mistake:
print print "ok"
First prints "ok" and then a number.
This also compiles:
print print print print "ok"
-
This is legit, strange as it may seem.
print print print print "ok"
The compiler sees it like this:
print print( print ( print( "ok") ) )
-
But this does not work.
int i = 12345
namespace aa
int i = 10
int x = 22
print global::i
end namespace
It prints 10 instead of 12345.
-
This is legit, strange as it may seem.
print print print print "ok"
The compiler sees it like this:
print print( print ( print( "ok") ) )
Do you mean that print is a function instead of an statement?
-
Yes, print is an overrideable core procedure, not a statement.
-
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.
-
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
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.
-
I heard the word VARIANT twice in that post. Are you getting close with a COM O2 solution?
-
I can't do anything until my suggested changes regarding constructors and transient objects are implemented.
-
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 '..'
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
-
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.
#unique on
enum bit ee
a,b,c,d
end enum
print ee.c '4
#unique off
enum bit ee
a,b,c,d
end enum
print c '4
-
An example that relays back to the core print function:
function print(float a,b)
=========================
print str(a,b) 'relay to core print function
end function
'#recordof print
print pi(),5
print "ok"
-
Another example. using a class or UDT:
'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"