Welcome to Phrogram Sign in | Join | Help
in Search


Bug#37: Constants in array definitions cannot be used at global scope

Last post 08-26-2008, 3:28 PM by The Dragon Rider. 7 replies.
Sort Posts: Previous Next
  •  08-13-2008, 2:14 AM 7060

    Bug#37: Constants in array definitions cannot be used at global scope

    Not sure if these are bugs or just mis-use but at least one of them seems to contradict the Help File. Given the following program:

    Program ShowProblems
       
        Constant MaxTasks As Integer = 100
        Define TaskList As TaskRecord [ MaxTasks ]
       
        Class TaskRecord

            Define TaskValue As Integer = 0
           
            Method setValue (val As Integer)
                This.TaskValue = val
            End Method
           
            Function getValue() As Integer
                Return This.TaskValue
            End Function
           
        End Class

        Method Main()

            Define task As TaskRecord
            Define I As Integer
           
            For I = 1 To 3
                task.setValue(I)
                TaskList[I] = task
            Next
           
            For I = 1 To 3
                task = TaskList[I]
                PrintLine("Entry " + I + " has value of " + task.getValue())
            Next

        End Method

    End Program

     There are two issues:

     

    1.   Although the 'compiler' accepts the constant MaxTasks as the dimension for the array on line 4, at run time the program stops at this line saying that 'Index was outside the bounds of the array'. If I change to using the value 100 in the array declaration then it works fine.

    2.   How do class assignments work in Phrogram? It seems that they work by reference rathr than value since the final loop in the above prints out the following:

    Entry 1 has value of 3
    Entry 2 has value of 3
    Entry 3 has value of 3

    which suggests that all three entries in the array are 'pointing' at the same instance of TaskRecord (i.e. task). This is not necessarily wrong but it is a critical thing to know and I could not find any reference to how class/structure assignmenst work anywhere.

    Please clarify both above points please.

     

    Thanks,   Chris

     

     

     

     

  •  08-13-2008, 7:48 AM 7061 in reply to 7060

    Re: Possible bugs?

    #1 seems like a valid bug. If TaskList is defined anywhere but the global scope then the constant in the declaration works fine

    #2 You are only ever creating one TaskRecord - the implicit one that we create for you when you declare it. Change your set loop to this:

            For I = 1 To 3
                task = New TaskRecord()
                task.setValue(I)
                TaskList[I] = task
            Next

    In C# your app would have failed becuase declaring a varialbe as type TaskRecord doesn't actually create an instance so you would have got a 'Object is null' error. In Phrogram we decided that the declaration will always create an instance, however just like other .Net languages it is just the one instance so as you found the same instance was being used in all 3 array slots.

    i.e

    Phrogram:
    Define task as TaskRecord

    is the same as C#
    TaskRecord task = new TaskRecord()

     


    Managed DirectX and XNA ? Check out http://www.thezbuffer.com
  •  08-13-2008, 12:58 PM 7064 in reply to 7061

    Re: Possible bugs?

    #1 - nice bug.. ;-)

    The issue is that the arrays get created before the constant value is set. So under the covers its trying to create a 0 length array. I'm not 100% sure what is causing it but there can be other problems too. The phrogram below doesn't give an error but you can see that the integer array is being created with a length of zero.

    Right now I can't find a workaround other than avoiding the mix of constants in global array definitions

     

    Program ShowProblems
       
        Constant Count As Integer = 100
        Define intList As Integer [ Count ]
        Define goodList As myClass[ 100 ]
        //Define List As myClass [ Count ]
       
        Class myClass

          
        End Class

        Method Main()

          Define otherGoodList As myClass[Count]

              Console.WriteLine(ArrayLength(otherGoodList))
              Console.WriteLine(ArrayLength(goodList))
              Console.WriteLine(ArrayLength(intList))

        End Method

    End Program

     


    Managed DirectX and XNA ? Check out http://www.thezbuffer.com
  •  08-14-2008, 4:11 AM 7067 in reply to 7061

    Re: Possible bugs?

    Thanks for the reply. For #2, this seems reasonable; it is similar to C++. I was just looking for confirmation of the behaviour. Also, I was looking for something like the 'new' operator but I couldn't find it anywhere in the help...

     

  •  08-17-2008, 12:26 PM 7077 in reply to 7067

    Re: Possible bugs?

    New object(args)
  •  08-25-2008, 2:37 AM 7091 in reply to 7077

    Re: Possible bugs?

    What are the 'args' in NEW object(args)? Can a class definitions take arguments? I don't see any option to declare a constructor in Phrogram, though this are seems very poorly documented.

     While I am at it, how are dynamically allocated objects freed in Phrogram. Is there an explicit 'delete' type operator as in C++ or is it done by backgroung garbage collection as in Java or ...

    Thanks,   Chris

     

  •  08-26-2008, 11:51 AM 7093 in reply to 7091

    Re: Possible bugs?

    On vacation right now so limited information at hand.

    I don't beleive Phrogram supports parameterized constructors - in fact I am not sure that the constructors are supported (though they work - I think its on of 'those' features'). Im sure the others will reply and confirm me or not but the object support in Phorgram is simple by design. e.g. we dont require a 'new' unless you need extra objects.

    Phrogram is a .Net languages and thereforw uses the .Net GC for all memory reclamation. Interestingly enough very little of the GC is actually done in the background. Its actually run on the same thread as your application and your app stops running for a very short period of time.


    Managed DirectX and XNA ? Check out http://www.thezbuffer.com
  •  08-26-2008, 3:28 PM 7094 in reply to 7093

    Re: Possible bugs?

    Constructors will accept arguments just like a method:

    Class Thing

    Var Name as String

    Constructor ( NameOfObject as String )

    Name = NameofObject

    End Constructor

    End Class

    Var Toy as Thing = New Thing ( "Car" )

     This is how most Class constructors are used. As ZMan stated they work but are not supported, so they may have unexpected bugs etc, but so far they have behaved nicely.


View as RSS news feed in XML