lib: remove usage of old-style getters and setters
authorJean Privat <jean@pryen.org>
Tue, 15 Apr 2014 12:59:55 +0000 (08:59 -0400)
committerJean Privat <jean@pryen.org>
Tue, 15 Apr 2014 13:52:23 +0000 (09:52 -0400)
old-style attributes are still available but getters and setters are manual.
This encourage the use of new-style attributes.

Support for old style attributes will be removed eventually.

Signed-off-by: Jean Privat <jean@pryen.org>

lib/dummy_array.nit
lib/filter_stream.nit
lib/opts.nit
lib/standard/collection/abstract_collection.nit
lib/standard/collection/array.nit
lib/standard/collection/hash_collection.nit
lib/standard/collection/list.nit
lib/standard/collection/range.nit
lib/standard/file.nit
lib/standard/string_search.nit

index a0ea46c..f939e49 100644 (file)
@@ -4,7 +4,7 @@
 #
 # This file is free software, which comes along with NIT.  This software is
 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
-# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A 
+# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A
 # PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
 # is kept unaltered, and a notification of the changes is added.
 # You  are  allowed  to  redistribute it and sell it, alone or is a part of
@@ -13,8 +13,9 @@
 class DummyArray
        super Set[Int]
        super ArrayCapable[Int]
-       var _capacity: Int 
-       redef readable var _length: Int 
+       var _capacity: Int
+       var _length: Int
+       redef fun length do return _length
        var _keys: NativeArray[Int]
        var _values: NativeArray[Int]
 
index a32c288..32e5728 100644 (file)
 class FilterIStream
        super IStream
        # Filter readed elements
-       readable var _stream: nullable IStream = null
+       var stream: nullable IStream = null
 
        redef fun eof: Bool
        do
                assert stream != null
                return stream.eof
        end
-
-       private fun stream=(i: nullable IStream)
-       do
-               _stream = i
-       end
 end
 
 class FilterOStream
        super OStream
        # Filter outputed elements
-       readable var _stream: nullable OStream = null
+       var stream: nullable OStream = null
 
        # Can the stream be used to write
        redef fun is_writable: Bool
@@ -39,11 +34,6 @@ class FilterOStream
                assert stream != null
                return stream.is_writable
        end
-
-       private fun stream=(i: OStream)
-       do
-               _stream = i
-       end
 end
 
 class StreamCat
@@ -65,12 +55,14 @@ class StreamCat
 
        redef fun stream: nullable IStream
        do
-               if _stream == null and _streams.is_ok then
-                       stream = _streams.item
-                       assert _stream != null
+               var res = super
+               if res == null and _streams.is_ok then
+                       res = _streams.item
+                       stream = res
+                       assert stream != null
                        _streams.next
                end
-               return _stream
+               return res
        end
 
        redef fun read_char: Int
index 174ad63..6b3313e 100644 (file)
@@ -5,7 +5,7 @@
 #
 # This file is free software, which comes along with NIT.  This software is
 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
-# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A 
+# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A
 # PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
 # is kept unaltered, and a notification of the changes is added.
 # You  are  allowed  to  redistribute it and sell it, alone or is a part of
@@ -17,50 +17,47 @@ module opts
 # Super class of all option's class
 abstract class Option
        # Names for the option (including long and short ones)
-       readable var _names: Array[String]
+       var names: Array[String]
 
        # Type of the value of the option
        type VALUE: nullable Object
 
        # Human readable description of the option
-       readable var _helptext: String 
+       var helptext: String
 
        # Gathering errors during parsing
-       readable var _errors: Array[String]
+       var errors: Array[String]
 
        # Is this option mandatory?
-       readable writable var _mandatory: Bool 
+       var mandatory: Bool writable
 
        # Has this option been read?
-       readable var _read:Bool
+       var read:Bool writable
 
        # Current value of this option
-       writable var _value: nullable VALUE
-
-       # Current value of this option
-       fun value: VALUE do return _value.as(VALUE)
+       var value: VALUE writable
 
        # Default value of this option
-       readable writable var _default_value: nullable VALUE
+       var default_value: nullable VALUE writable
 
        # Create a new option
        init init_opt(help: String, default: nullable VALUE, names: nullable Array[String])
        do
                if names == null then
-                       _names = new Array[String]
+                       self.names = new Array[String]
                else
-                       _names = names.to_a
+                       self.names = names.to_a
                end
-               _helptext = help
-               _mandatory = false
-               _read = false
-               _default_value = default
-               _value = default 
-               _errors = new Array[String]
+               helptext = help
+               mandatory = false
+               read = false
+               default_value = default
+               value = default
+               errors = new Array[String]
        end
 
        # Add new aliases for this option
-       fun add_aliases(names: String...) do _names.add_all(names)
+       fun add_aliases(names: String...) do names.add_all(names)
        
        # An help text for this option with default settings
        redef fun to_s do return pretty(2)
@@ -69,7 +66,7 @@ abstract class Option
        fun pretty(off: Int): String
        do
                var text = new FlatBuffer.from("  ")
-               text.append(_names.join(", "))
+               text.append(names.join(", "))
                text.append("  ")
                var rest = off - text.length
                if rest > 0 then text.append(" " * rest)
@@ -88,7 +85,7 @@ abstract class Option
        # Consume parameters for this option
        protected fun read_param(it: Iterator[String])
        do
-               _read = true
+               read = true
        end
 end
 
@@ -133,7 +130,7 @@ abstract class OptionParameter
        protected fun convert(str: String): VALUE is abstract
 
        # Is the parameter mandatory?
-       readable writable var _parameter_mandatory: Bool
+       var parameter_mandatory: Bool writable
 
        redef fun read_param(it)
        do
@@ -142,8 +139,8 @@ abstract class OptionParameter
                        value = convert(it.item)
                        it.next
                else
-                       if _parameter_mandatory then
-                               _errors.add("Parameter expected for option {names.first}.")
+                       if parameter_mandatory then
+                               errors.add("Parameter expected for option {names.first}.")
                        end
                end
        end
@@ -151,7 +148,7 @@ abstract class OptionParameter
        init init_opt(h, d, n)
        do
                super
-               _parameter_mandatory = true
+               parameter_mandatory = true
        end
 end
 
@@ -167,22 +164,22 @@ end
 class OptionEnum
        super OptionParameter
        redef type VALUE: Int
-       var _values: Array[String]
+       var values: Array[String]
 
        init(values: Array[String], help: String, default: Int, names: String...)
        do
                assert values.length > 0
-               _values = values.to_a
+               self.values = values.to_a
                init_opt("{help} <{values.join(", ")}>", default, names)
        end
 
        redef fun convert(str)
        do
-               var id = _values.index_of(str)
+               var id = values.index_of(str)
                if id == -1 then
-                       var e = "Unrecognized value for option {_names.join(", ")}.\n"
-                       e += "Expected values are: {_values.join(", ")}."
-                       _errors.add(e)
+                       var e = "Unrecognized value for option {names.join(", ")}.\n"
+                       e += "Expected values are: {values.join(", ")}."
+                       errors.add(e)
                end
                return id
        end
@@ -192,7 +189,7 @@ class OptionEnum
        redef fun pretty_default
        do
                if default_value != null then
-                       return " ({_values[default_value.as(not null)]})"
+                       return " ({values[default_value.as(not null)]})"
                else
                        return ""
                end
@@ -214,29 +211,29 @@ class OptionArray
 
        init(help: String, names: String...)
        do
-               _values = new Array[String]
-               init_opt(help, _values, names)
+               values = new Array[String]
+               init_opt(help, values, names)
        end
 
-       var _values: Array[String]      
+       private var values: Array[String]
        redef fun convert(str)
        do
-               _values.add(str)
-               return _values
+               values.add(str)
+               return values
        end
 end
 
 class OptionContext
-       readable var _options: Array[Option] 
-       readable var _rest: Array[String] 
-       readable var _errors: Array[String]
+       var options: Array[Option]
+       var rest: Array[String]
+       var errors: Array[String]
 
-       var _optmap: Map[String, Option]
+       private var optmap: Map[String, Option]
        
        fun usage
        do
                var lmax = 1
-               for i in _options do
+               for i in options do
                        var l = 3
                        for n in i.names do
                                l += n.length + 2
@@ -244,7 +241,7 @@ class OptionContext
                        if lmax < l then lmax = l
                end
                
-               for i in _options do
+               for i in options do
                        print(i.pretty(lmax))
                end
        end
@@ -260,7 +257,7 @@ class OptionContext
        do
                var parseargs = true
                build
-               var rest = _rest
+               var rest = rest
 
                while parseargs and it.is_ok do
                        var str = it.item
@@ -274,8 +271,8 @@ class OptionContext
                                        var next_called = false
                                        for i in [1..str.length] do
                                                var short_opt = "-" + str.chars[i].to_s
-                                               if _optmap.has_key(short_opt) then
-                                                       var option = _optmap[short_opt]
+                                               if optmap.has_key(short_opt) then
+                                                       var option = optmap[short_opt]
                                                        if option isa OptionParameter then
                                                                it.next
                                                                next_called = true
@@ -285,8 +282,8 @@ class OptionContext
                                        end
                                        if not next_called then it.next
                                else
-                                       if _optmap.has_key(str) then
-                                               var opt = _optmap[str]
+                                       if optmap.has_key(str) then
+                                               var opt = optmap[str]
                                                it.next
                                                opt.read_param(it)
                                        else
@@ -297,9 +294,9 @@ class OptionContext
                        end
                end
 
-               for opt in _options do
+               for opt in options do
                        if opt.mandatory and not opt.read then
-                               _errors.add("Mandatory option {opt.names.join(", ")} not found.")
+                               errors.add("Mandatory option {opt.names.join(", ")} not found.")
                        end
                end
        end
@@ -307,23 +304,23 @@ class OptionContext
        fun add_option(opts: Option...)
        do
                for opt in opts do
-                       _options.add(opt)
+                       options.add(opt)
                end
        end
 
        init
        do
-               _options = new Array[Option]
-               _optmap = new HashMap[String, Option]
-               _rest = new Array[String]
-               _errors = new Array[String]
+               options = new Array[Option]
+               optmap = new HashMap[String, Option]
+               rest = new Array[String]
+               errors = new Array[String]
        end
 
        private fun build
        do
-               for o in _options do
+               for o in options do
                        for n in o.names do
-                               _optmap[n] = o
+                               optmap[n] = o
                        end
                end
        end
@@ -332,9 +329,9 @@ class OptionContext
        do
                var errors: Array[String] = new Array[String]
 
-               errors.add_all(_errors)
+               errors.add_all(errors)
 
-               for o in _options do
+               for o in options do
                        for e in o.errors do
                                errors.add(e)
                        end
index 20be476..dbed6d6 100644 (file)
@@ -163,19 +163,19 @@ end
 class Container[E]
        super Collection[E]
 
-       redef fun first do return _item
+       redef fun first do return item
 
        redef fun is_empty do return false
 
        redef fun length do return 1
 
-       redef fun has(an_item) do return _item == an_item
+       redef fun has(an_item) do return item == an_item
 
-       redef fun has_only(an_item) do return _item == an_item
+       redef fun has_only(an_item) do return item == an_item
 
        redef fun count(an_item)
        do
-               if _item == an_item then
+               if item == an_item then
                        return 1
                else
                        return 0
@@ -185,10 +185,10 @@ class Container[E]
        redef fun iterator do return new ContainerIterator[E](self)
 
        # Create a new instance with a given initial value.
-       init(e: E) do _item = e
+       init(e: E) do item = e
 
        # The stored item
-       readable writable var _item: E
+       var item: E writable
 end
 
 # This iterator is quite stupid since it is used for only one item.
@@ -196,11 +196,11 @@ private class ContainerIterator[E]
        super Iterator[E]
        redef fun item do return _container.item
 
-       redef fun next do _is_ok = false
+       redef fun next do is_ok = false
 
        init(c: Container[E]) do _container = c
 
-       redef readable var _is_ok: Bool = true
+       redef var is_ok: Bool = true
 
        var _container: Container[E]
 end
@@ -900,15 +900,15 @@ end
 class Couple[F, S]
 
        # The first element of the couple.
-       readable writable var _first: F
+       var first: F writable
 
        # The second element of the couple.
-       readable writable var _second: S
+       var second: S writable
 
        # Create a new instance with a first and a second object.
        init(f: F, s: S)
        do
-               _first = f
-               _second = s
+               first = f
+               second = s
        end
 end
index 6fe304e..d40a220 100644 (file)
@@ -21,7 +21,8 @@ import abstract_collection
 abstract class AbstractArrayRead[E]
        super SequenceRead[E]
 
-       redef readable var _length: Int = 0
+       var _length: Int = 0
+       redef fun length do return _length
 
        redef fun is_empty do return _length == 0
 
@@ -361,7 +362,8 @@ private class ArrayIterator[E]
                _index = 0
        end
 
-       redef readable var _index: Int = 0
+       var _index: Int = 0
+       redef fun index do return _index
        var _array: AbstractArrayRead[E]
 end
 
index 5406f40..c2c8353 100644 (file)
@@ -23,7 +23,7 @@ private abstract class HashCollection[K: Object, N: HashNode[Object]]
        var _capacity: Int = 0 # Size of _array
        var _length: Int = 0 # Number of items in the map
 
-       readable var _first_item: nullable N = null # First added item (used to visit items in nice order)
+       var _first_item: nullable N = null # First added item (used to visit items in nice order)
        var _last_item: nullable N = null # Last added item (same)
 
        # The last key accessed (used for cache)
@@ -189,8 +189,8 @@ end
 private abstract class HashNode[K: Object]
        var _key: K
        type N: HashNode[K]
-       readable writable var _next_item: nullable N = null
-       readable writable var _prev_item: nullable N = null
+       var _next_item: nullable N = null
+       var _prev_item: nullable N = null
        var _prev_in_bucklet: nullable N = null
        var _next_in_bucklet: nullable N = null
        init(k: K)
@@ -387,7 +387,7 @@ class HashMapIterator[K: Object, V]
        init(map: HashMap[K, V])
        do
                _map = map
-               _node = map.first_item
+               _node = map._first_item
        end
 end
 
index 0c5ce5f..c5dc5dd 100644 (file)
@@ -301,7 +301,9 @@ class ListIterator[E]
        var _node: nullable ListNode[E]
 
        # The index of the current node
-       redef readable var _index: Int
+       var _index: Int
+
+       redef fun index do return _index
 
        # Remove the current item
        fun delete
@@ -342,8 +344,8 @@ private class ListNode[E]
        end
 
        # The next node.
-       readable writable var _next: nullable ListNode[E]
+       var next: nullable ListNode[E]
 
        # The previous node.
-       readable writable var _prev: nullable ListNode[E]
+       var prev: nullable ListNode[E]
 end
index 3c7157f..e72771e 100644 (file)
@@ -19,17 +19,17 @@ import abstract_collection
 class Range[E: Discrete]
        super Collection[E]
 
-       redef readable var _first: E
+       redef var first: E
 
        # Get the last element.
-       readable var _last: E
+       var last: E
 
        # Get the element after the last one.
-       readable var _after: E
+       var after: E
 
-       redef fun has(item) do return item >= _first and item <= _last
+       redef fun has(item) do return item >= first and item <= last
 
-       redef fun has_only(item) do return _first == item and item == _last or is_empty
+       redef fun has_only(item) do return first == item and item == last or is_empty
 
        redef fun count(item)
        do
@@ -44,7 +44,7 @@ class Range[E: Discrete]
 
        redef fun length
        do
-               var nb = _first.distance(_after)
+               var nb = first.distance(after)
                if nb > 0 then
                        return nb
                else
@@ -52,24 +52,24 @@ class Range[E: Discrete]
                end
        end
 
-       redef fun is_empty do return _first >= _after
+       redef fun is_empty do return first >= after
 
        # Create a range [`from`, `to`].
        # The syntax `[from..to[` is equivalent.
        init(from: E, to: E)
        do
-               _first = from
-               _last = to
-               _after = to.successor(1)
+               first = from
+               last = to
+               after = to.successor(1)
        end
 
        # Create a range [`from`, `to`[.
        # The syntax `[from..to[` is equivalent.
        init without_last(from: E, to: E)
        do
-               _first = from
-               _last = to.predecessor(1)
-               _after = to
+               first = from
+               last = to.predecessor(1)
+               after = to
        end
 end
 
@@ -77,7 +77,8 @@ private class IteratorRange[E: Discrete]
        # Iterator on ranges.
        super Iterator[E]
        var _range: Range[E]
-       redef readable var _item: E
+       var _item: E
+       redef fun item do return _item
 
        redef fun is_ok do return _item < _range.after
        
index d6c800b..0b5fd04 100644 (file)
@@ -65,7 +65,7 @@ end
 abstract class FStream
        super IOS
        # The path of the file.
-       readable var _path: nullable String = null
+       var path: nullable String = null
 
        # The FILE *.
        var _file: nullable NativeFile = null
@@ -85,8 +85,8 @@ class IFStream
        fun reopen
        do
                if not eof then close
-               _file = new NativeFile.io_open_read(_path.to_cstring)
-               _end_reached = false
+               _file = new NativeFile.io_open_read(path.to_cstring)
+               end_reached = false
                _buffer_pos = 0
                _buffer.clear
        end
@@ -94,14 +94,14 @@ class IFStream
        redef fun close
        do
                var i = _file.io_close
-               _end_reached = true
+               end_reached = true
        end
 
        redef fun fill_buffer
        do
                var nb = _file.io_read(_buffer.items, _buffer.capacity)
                if nb <= 0 then
-                       _end_reached = true
+                       end_reached = true
                        nb = 0
                end
                _buffer.length = nb
@@ -109,14 +109,14 @@ class IFStream
        end
        
        # End of file?
-       redef readable var _end_reached: Bool = false
+       redef var end_reached: Bool = false
 
        # Open the file at `path` for reading.
        init open(path: String)
        do
-               _path = path
+               self.path = path
                prepare_buffer(10)
-               _file = new NativeFile.io_open_read(_path.to_cstring)
+               _file = new NativeFile.io_open_read(path.to_cstring)
                assert cant_open_file: _file != null
        end
 
@@ -162,7 +162,7 @@ class OFStream
        do
                _file = new NativeFile.io_open_write(path.to_cstring)
                assert cant_open_file: _file != null
-               _path = path
+               self.path = path
                _writable = true
        end
        
@@ -176,7 +176,7 @@ class Stdin
        super IFStream
        private init do
                _file = new NativeFile.native_stdin
-               _path = "/dev/stdin"
+               path = "/dev/stdin"
                prepare_buffer(1)
        end
 
@@ -189,7 +189,7 @@ class Stdout
        super OFStream
        private init do
                _file = new NativeFile.native_stdout
-               _path = "/dev/stdout"
+               path = "/dev/stdout"
                _writable = true
        end
 end
@@ -198,7 +198,7 @@ class Stderr
        super OFStream
        private init do
                _file = new NativeFile.native_stderr
-               _path = "/dev/stderr"
+               path = "/dev/stderr"
                _writable = true
        end
 end
index 65f3292..8dcf90a 100644 (file)
@@ -238,20 +238,20 @@ end
 # Matches are a part of a `Text` found by a `Pattern`.
 class Match
        # The base string matched
-       readable var _string: String
+       var string: String
 
        # The starting position in the string
-       readable var _from: Int
+       var from: Int
 
        # The length of the matching part
-       readable var _length: Int
+       var length: Int
 
        # The position of the first character just after the matching part.
        # May be out of the base string
-       fun after: Int do return _from + _length
+       fun after: Int do return from + length
 
        # The contents of the matching part
-       redef fun to_s do return _string.substring(_from,_length)
+       redef fun to_s do return string.substring(from,length)
 
        # Matches `len` characters of `s` from `f`.
        init(s: String, f: Int, len: Int)
@@ -259,9 +259,9 @@ class Match
                assert positive_length: len >= 0
                assert valid_from: f >= 0
                assert valid_after: f + len <= s.length
-               _string = s
-               _from = f
-               _length = len
+               string = s
+               from = f
+               length = len
        end
 end