0

How do I declare an array of Strings in a getter / method declaration ?

Ex ( none of these exemples works ) :

class Test
    getter var1 : String[]
    getter var2 : String*
    getter var3 : Array(String)
end

1 Answers1

3
getter var3 : Array(String)

This is the correct way to declare an instance variable as an Array of String. However, you'll need to assign a value, either in the declaration or in the constructor.

If it should be an empty Array by Default, you can use

getter var3 = Array(String).new 
# or
getter var3 = [] of String

If you need the variable to allow a nil value, you can use

getter var3 : Array(String)?
Johannes Müller
  • 5,581
  • 1
  • 11
  • 25