Arrays of an Interface type variable?

Monkey Forums/Monkey Programming/Arrays of an Interface type variable?

Nobuyuki(Posted 2013) [#1]
Hello,

When I define an array of an interface type as a field in my class (eg: Field myVar:InterfaceType[]), attempting to reference a known method of one of the items in the array later throws the compile-time error "Expecting identifier". Is Monkey unable to access members of an interface if it's part of an array?

I'm assuming that this problem doesn't exist if using a Stack<InterfaceType> instead of an array, but I don't want to incur whatever overhead that may entail...


dragon(Posted 2013) [#2]
use class, not interface
interface is a "template" for class


Goodlookinguy(Posted 2013) [#3]
@dragon

No, not really. Interfaces are much more than that.

@Nobuyuki

I made and tried some test code using an array of interfaces and it worked just fine here on v70g.

This is the code I used:

Interface ICook
	Method Bake:Void()
End

Class CakeShop Implements ICook
	Method Bake:Void()
		Print "Hi"
	End
End

Class Example
	Field example:ICook[]
	
	Method New()
		example = New ICook[10]
		
		example[0] = New CakeShop()
	End
End

Function Main:Int()
	Local blah := New Example()
	
	blah.example[0].Bake()
End



dragon(Posted 2013) [#4]
interesting...
i never used interfaces like this...



but Monkey Docs say:

An interface can be used where ever a class is expected, for example when declaring the types of variables, or function return types. An interface cannot however be used with New.



Nobuyuki(Posted 2013) [#5]
Okay, I figured out the problem. I was using a reserved keyword as a Method name. Derp!! Late night debugging...


Goodlookinguy(Posted 2013) [#6]
@dragon

An interface can be used where ever a class is expected, for example when declaring the types of variables, or function return types.

An interface is meant as an abstract way to attach components. Like you would make a database interface. Then all databases would use that interface to communicate with the system.

The usefulness of interfaces is for interacting components that otherwise would have to have specific code for each class that interacts.

An interface cannot however be used with New.

This isn't written very well. What it's saying is that you can't have a method 'New' in an interface.

e.g.
Interface ITest
    Method New()
End


@Nobuyuki

I know that feeling, well, except that late night is when I do my best work. I have early morning debugging problems.