bmx-ng / bcc

A next-generation bcc parser for BlitzMax
zlib License
33 stars 12 forks source link

Module scoped objects with 'New' #580

Open davecamp opened 2 years ago

davecamp commented 2 years ago

Is module scope taken into account with respect to Types within modules? It works Ok for Global variables - Ie If a Global variable is in a module and a Global variable is in the main code file you can resolve module resolution by:

SuperStrict
Global Variable:Int = 20
Print (Some.Module.Variable) ' Print the value of a global that is defined in the mentioned module
Print (Variable)             ' Print the value of the global in this file

There appears to be an issue when doing the same for Types

SuperStrict

Type Foo
    Field Bar:Float
EndType

' any valid module name can be used here - even if Foo isn't inside it
Local Test:Foo = New Some.Module.Foo    ' Some.Module can be exchanged for any valid module name

There are a couple of things kind of expected to happen with that last code.

  1. There is no type in Some.Module.Foo so an error is expected to say this.
  2. If the module did have a Type called Foo within it then the module version is ignored.
  3. Trying to cast from the Some.Module.Foo to the local type of Foo should produce an error.
  4. If module scoped resolution for types isn't allowed then a general error would be produced in regards to a duplicate type.

The code above compiles and runs creating an instance of Foo using the local version.

HurryStarfish commented 2 years ago

On my BlitzMax-NG installation from June 2021, it behaves as expected:

SuperStrict
Framework Brl.Blitz

Type T End Type
Type TBlitzException Extends T
    Field f:Int
End Type

Local o1:TBlitzException = New TBlitzException ' resolves to local type
Local o2:BRL.Blitz.TBlitzException = New BRL.Blitz.TBlitzException ' resolves to type from module
'Local o2:TBlitzException = New BRL.Blitz.TBlitzException ' compile error
'Local o3:Object = New BRL.Blitzasdf.TBlitzException ' compile error
DebugStop

This must be a more recently introduced bug then.

davecamp commented 2 years ago

@HurryStarfish Your example also works as expected for me too. What do you get if you try this one?

SuperStrict

Type Foo
    Field Bar:Float
EndType

Local Test:Foo = New Brl.Blitz.Foo
DebugStop
HurryStarfish commented 2 years ago

Ah, yes. Apparently, instead of causing an error the module namespace is completely ignored if it refers to a module that is being imported and the member is not found in that namespace. Whether the local scope has something with the same name doesn't seem to make a difference.

SuperStrict
Framework BRL.StandardIO

Local Test:Object = New BRL.StandardIO.TRuntimeException ' creates a BRL.Blitz.TRuntimeException
BRL.StandardIO.RuntimeError "test" ' calls a BRL.Blitz.RuntimeError