The general transition to new constructor is nearer each day.
May fix an issue informally signaled by @R4PaSs.
Pull-Request: #783
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
# Grid of monsters.
class Grid
# width of the current grid
- var width: Int
+ var width: Int is noinit
# maximum width of the grid
var max_width: Int
# height of the current grid
- var height: Int
+ var height: Int is noinit
# maximum height of the grid
var max_height: Int
# the data grid
private var grid = new Array[Array[Tile]]
- init(mw,mh,nm: Int)
- do
- self.max_width = mw
- self.max_height = mh
- self.nb_monsters = mh
- clear
- end
+ init do clear
# Reinitialize the grid with new empty tiles and monsters info
fun clear
end
class BasicBlock
- var name : String
+ var name: String is noinit
var lines = new Array[ANonEmptyLine]
var successors = new Array[BasicBlock]
var predecessors = new Array[BasicBlock]
private class CircularListIterator[E]
super IndexedIterator[E]
- redef var index: Int
+ redef var index: Int = 0
# The current node pointed.
# Is null if the list is empty.
- var node: nullable CLNode[E]
+ var node: nullable CLNode[E] is noinit
# The list iterated.
var list: CircularList[E]
redef fun item do return self.node.item
- init(list: CircularList[E])
+ init
do
self.node = list.node
- self.list = list
- self.index = 0
end
end
# The Android MediaPlayer has a complex state diagram that you'll need to
# respect if you want your MediaPlayer to work fine, see the android doc
class MediaPlayer
- private var nmedia_player: NativeMediaPlayer
+ private var nmedia_player: NativeMediaPlayer is noinit
# The sound associated with this mediaplayer
- var sound: nullable Sound
+ var sound: nullable Sound = null
# Create a new MediaPlayer, but no sound is attached, you'll need
# to use `load_sound` before using it
# Abuser iterator to read a file, see `file_open`
private class ReadFileForAbuserIterator
super Iterator[IFStream]
- redef var item: IFStream
+ var path: String
+ redef var item: IFStream is noinit
redef var is_ok = true
- init(path: String)
+ init
do
# start of service is to open the file, and return in
item = new IFStream.open(path)
private class SortAbuserIterator[E]
super Iterator[CompareQuery[E]]
# The index of the big loop
- var i: Int
+ var i: Int = 0
# The index of the small loop
- var j: Int
+ var j: Int = 0
# The array to sort
var array: Array[E]
# The query used to communicate with the user.
# For ecological concerns, a unique CompareQuery is instatiated.
- var query: nullable CompareQuery[E]
+ var query: nullable CompareQuery[E] = null
redef fun item do return query.as(not null)
- init(array: Array[E])
+ init
do
- self.array = array
# Initialize the algorithm, see `next` for the rest
- i = 0
- j = 0
if not is_ok then return
query = new CompareQuery[E](array[i], array[j])
end
redef var x: N
redef var y: N
-
- init(x, y: N)
- do
- self.x = x
- self.y = y
- end
end
# An abstract 3d point, strongly linked to its implementation `Point3d`
super Point[N]
redef var z: N
-
- init(x, y, z: N)
- do
- super
- self.z = z
- end
end
# An abstract 2d line segment
redef var point_left: P
redef var point_right: P
- init(a, b: P)
+ init
do
- if a.x < b.x then
- point_left = a
- point_right = b
- else
+ var a = point_left
+ var b = point_right
+ if a.x > b.x then
point_left = b
point_right = a
end
# The height of a tile
var height: Int
- init(image: Image, width: Int, height: Int)
+ init
do
- self.image = image
- self.width = width
- self.height = height
-
self.nb_cols = image.width / width
self.nb_rows = image.height / height
end
# The number of columns of tiles in the image
- var nb_cols: Int
+ var nb_cols: Int is noinit
# The number of rows of tiles in the image
- var nb_rows: Int
+ var nb_rows: Int is noinit
# Cache for images of tiles
var subimages = new Array[Image]
# Use space (' ') for holes in the tileset
var chars: String
- init(image: Image, width: Int, height: Int, chars: String)
- do
- super
- self.chars = chars
- end
-
# Additional space to insert horizontally between characters
# A negave value will display tile overlaped
var hspace: Int = 0 is writable
# For more details, see: http://docs.neo4j.org/chunked/milestone/rest-api-cypher.html
class CypherQuery
# Query string to perform
- private var query: String
+ private var query: String = ""
# `params` to embed in the query like in prepared statements
var params = new JsonObject
- init do
- self.query = ""
- end
+ init do end
# init the query from a query string
init from_string(query: String) do
# assert node["name"] == "Andres" # loaded lazily from base
abstract class NeoEntity
# Neo4j client connector
- private var neo: Neo4jClient
+ private var neo: Neo4jClient is noinit
# Entity unique URL in Neo4j REST API
- var url: nullable String
+ var url: nullable String = null
# Temp id used in batch mode to update the entity
private var batch_id: nullable Int = null
super Visitor
var writer: OStream
private var indent = 0
- init(writer: OStream) do self.writer = writer
redef fun visit(n)
do
for i in [0..indent[ do writer.write(" ")
var source: Iterator[E]
var skip_item: E
- init(source: Iterator[E], skip_item: E)
- do
- self.source = source
- self.skip_item = skip_item
-
- do_skip
- end
+ init do do_skip
fun do_skip
do
var lasts = new List[E]
- init(source: Iterator[E], length: Int)
+ init
do
- self.source = source
- self.length = length
var lasts = self.lasts
while source.is_ok and lasts.length < length do
lasts.push(source.item)
var predicate: Function[E, Bool]
- init(source: Iterator[E], predicate: Function[E, Bool])
- do
- self.source = source
- self.predicate = predicate
-
- do_skip
- end
+ init do do_skip
fun do_skip
do
# IPv4 address the socket is connected to
# Formatted as xxx.xxx.xxx.xxx
- var address: String
+ var address: String is noinit
# Hostname of the socket connected to self
# In C : The real canonical host name (e.g. example.org)
- var host: nullable String
+ var host: nullable String = null
# Port open for the socket
- var port: Int
+ var port: Int is noinit
# Underlying C socket
- private var socket: FFSocket
+ private var socket: FFSocket is noinit
# Underlying C socket
- private var addrin: FFSocketAddrIn
+ private var addrin: FFSocketAddrIn is noinit
redef var end_reached = false
super Set[E]
# The stored elements.
- private var array: Array[E]
+ private var array: Array[E] is noinit
redef fun has(e) do return _array.has(e)
init from(coll: Collection[E]) do append(coll)
# The first node of the list
- private var head: nullable ListNode[E]
+ private var head: nullable ListNode[E] = null
# The last node of the list
- private var tail: nullable ListNode[E]
+ private var tail: nullable ListNode[E] = null
# Get the `i`th node. get `null` otherwise.
private fun get_node(i: Int): nullable ListNode[E]
class IProcess
super Process
super IStream
- var stream_in: FDIStream
+ var stream_in: FDIStream is noinit
redef fun close do stream_in.close
redef fun pipeflags do return 2
- redef init(command: String, arguments: String...) do super
-
redef fun execute
do
super
class OProcess
super Process
super OStream
- var stream_out: OStream
+ var stream_out: OStream is noinit
redef fun close do stream_out.close
redef fun pipeflags do return 1
- redef init(command: String, arguments: String...) do super
-
redef fun execute
do
super
redef fun pipeflags do return 3
- redef init(command: String, arguments: String...) do super
-
redef fun execute
do
super
super Text
# Root node, entry point of a Rope.
- private var root: RopeNode
+ private var root: RopeNode is noinit
# Cached version of self as a flat String
private var str_representation: nullable NativeString = null
private fun native_read(i: Int, buf: NativeString, len: Int): Int is extern "stream_FDStream_FDStream_native_read_3"
private fun native_write(i: Int, buf: NativeString, len: Int): Int is extern "stream_FDStream_FDStream_native_write_3"
private fun native_write_char(i: Int, c: Char): Int is extern "stream_FDStream_FDStream_native_write_char_2"
-
- init(fd: Int) do self.fd = fd
end
class FDIStream
if nb == -1 then eof = true
return nb
end
-
- init(fd: Int) do end
end
class FDOStream
super FDStream
super OStream
- redef var is_writable: Bool
+ redef var is_writable = true
redef fun write(s)
do
var nb = native_write(fd, s.to_cstring, s.length)
if nb < s.length then is_writable = false
end
-
- init(fd: Int)
- do
- is_writable = true
- end
end
class FDIOStream
super FDIStream
super FDOStream
super IOStream
- init(fd: Int)
- do
- self.fd = fd
- is_writable = true
- end
end
redef interface Object
#
# Warning : Might be void in some subclasses, be sure to check
# if set before using it.
- private var items: NativeString
+ private var items: NativeString is noinit
# Real items, used as cache for to_cstring is called
private var real_items: nullable NativeString = null
super String
# Index in _items of the start of the string
- private var index_from: Int
+ private var index_from: Int is noinit
# Indes in _items of the last item of the string
- private var index_to: Int
+ private var index_to: Int is noinit
redef var chars: SequenceRead[Char] = new FlatStringCharView(self)
# Are we a refinement
if not mclassdef.is_intro then return
- var mmodule = nclassdef.mclassdef.mmodule
-
# Look for the init in Object, or create it
if mclassdef.mclass.name == "Object" and the_root_init_mmethod == null then
# Create the implicit root-init method
if mpropdef.mproperty.is_root_init then
assert defined_init == null
defined_init = mpropdef
- else
- # An explicit old-style init, so return
+ else if mpropdef.mproperty.name == "init" then
+ # An explicit old-style init named "init", so return
return
end
end
if not nclassdef isa AStdClassdef then return
- # Do we inherit a old-style constructor?
- var combine = new Array[MMethod] # old-style constructors without arguments
- var inhc: nullable MClass = null # single super-class with a constructor with arguments
- if defined_init == null then for st in mclassdef.supertypes do
- var c = st.mclass
- if not c.kind.need_init then continue
- st = st.anchor_to(mmodule, mclassdef.bound_mtype)
- var candidate = self.try_get_mproperty_by_name2(nclassdef, mmodule, st, "init").as(nullable MMethod)
- if candidate != null then
- if candidate.is_root_init then continue
- if candidate.intro.msignature != null then
- if candidate.intro.msignature.arity == 0 then
- combine.add(candidate)
- continue
- end
- end
- end
- var inhc2 = c.inherit_init_from
- if inhc2 == null then inhc2 = c
- if inhc2 == inhc then continue
- if inhc != null then
- self.error(nclassdef, "Error: Cannot provide a defaut constructor: conflict for {inhc} and {c}")
- else
- inhc = inhc2
- end
- end
-
# Collect undefined attributes
var mparameters = new Array[MParameter]
var initializers = new Array[MProperty]
end
if anode == null then anode = nclassdef
- if combine.is_empty and inhc != null then
- if not mparameters.is_empty then
- self.error(anode,"Error: {mclassdef} cannot inherit constructors from {inhc} because there is attributes without initial values: {mparameters.join(", ")}")
- return
- end
-
- # TODO: actively inherit the consturctor
- self.toolcontext.info("{mclassdef} inherits all constructors from {inhc}", 3)
- #mclassdef.mclass.inherit_init_from = inhc
- #return
- end
-
- if not combine.is_empty and inhc != null then
- self.error(nclassdef, "Error: Cannot provide a defaut constructor: conflict for {combine.join(", ")} and {inhc}")
- return
- end
- if not combine.is_empty then
- if mparameters.is_empty and combine.length == 1 then
- # No need to create a local init, the inherited one is enough
- inhc = combine.first.intro_mclassdef.mclass
- mclassdef.mclass.inherit_init_from = inhc
- self.toolcontext.info("{mclassdef} inherits all constructors from {inhc}", 3)
- return
- end
- nclassdef.super_inits = combine
- var mprop = new MMethod(mclassdef, "init", mclassdef.mclass.visibility)
- var mpropdef = new MMethodDef(mclassdef, mprop, nclassdef.location)
- var msignature = new MSignature(mparameters, null)
- mpropdef.msignature = msignature
- mprop.is_init = true
- nclassdef.mfree_init = mpropdef
- self.toolcontext.info("{mclassdef} gets a free empty constructor {mpropdef}{msignature}", 3)
- return
- end
-
if the_root_init_mmethod == null then return
- # Look for nost-specific new-stype init definitions
+ # Look for most-specific new-stype init definitions
var spropdefs = the_root_init_mmethod.lookup_super_definitions(mclassdef.mmodule, mclassdef.bound_mtype)
if spropdefs.is_empty then
toolcontext.fatal_error(nclassdef.location, "Fatal error: {mclassdef} does not specialize {the_root_init_mmethod.intro_mclassdef}. Possible duplication of the root class `Object`?")
end
end
-redef class MClass
- # The class whose self inherit all the constructors.
- # FIXME: this is needed to implement the crazy constructor mixin thing of the of old compiler. We need to think what to do with since this cannot stay in the modelbuilder
- var inherit_init_from: nullable MClass = null
-end
-
redef class MPropDef
# Does the MPropDef contains a call to super or a call of a super-constructor?
# Subsequent phases of the frontend (esp. typing) set it if required
# receveur ("self")
# Les attributs sont déclarés par le mot clé "fun" et commencent par un "@"
- var nom_: String # Un entrepôt a un nom (de type chaîne).
- var rayons: Array[Rayon] # Il est composé d'un ensemble de rayon.
+ var nom_: String is noinit # Un entrepôt a un nom (de type chaîne).
+ var rayons: Array[Rayon] is noinit # Il est composé d'un ensemble de rayon.
# "Array" est une classe paramétrée, les crochets en sont la marque.
# La classe "Rayon" est définie plus loin
class Point
private
- var x: Int # Abscisse
- var y: Int # Ordonnée
+ var x: Int = 0 # Abscisse
+ var y: Int = 0 # Ordonnée
# Change la position d'un point
fun moveto(x: Int, y: Int)
end
- init
- do
- moveto(0, 0)
- end
-
init at(x: Int, y: Int)
do
moveto(x, y)
-alt/base_init_combine_alt1.nit:52,6: Error: base_init_combine_alt1#F cannot inherit constructors from E because there is attributes without initial values: z: Int
+1
+2
+2
+3
+2
+2
+5
+2
+6
-alt/error_init_auto_alt4.nit:35,5--12: Error: Incorrect number of parameters. Got 1, expected 0. Signature is
-alt/error_init_auto_alt4.nit:36,5--15: Error: Incorrect number of parameters. Got 2, expected 0. Signature is
-alt/error_init_auto_alt4.nit:37,5--18: Error: Incorrect number of parameters. Got 3, expected 0. Signature is
+alt/error_init_auto_alt4.nit:34,5--9: Error: Incorrect number of parameters. Got 0, expected 1. Signature is (x: Int)
+alt/error_init_auto_alt4.nit:36,5--15: Error: Incorrect number of parameters. Got 2, expected 1. Signature is (x: Int)
+alt/error_init_auto_alt4.nit:37,5--18: Error: Incorrect number of parameters. Got 3, expected 1. Signature is (x: Int)
end
class Toto
- var a: Int
+ var a: Int is noinit
redef fun output
do
printn(_a)
# limitations under the License.
class A
- var attribute: nullable A
- var num: Char
+ var attribute: nullable A is noinit
+ var num: Char is noinit
fun foo=(a: nullable A)
do