Skip to content

Commit

Permalink
[mojo-stdlib][******] Migrate to __init__(out self)
Browse files Browse the repository at this point in the history
This adopts the recent changes that allow the use of the `out`
argument convention.  This argument convention more correctly
models the nature of `__init__` which initializes self but never
reads from it.

This was discussed on this public thread:
#3623

MODULAR_ORIG_COMMIT_REV_ID: d05be1ccb28b254aeccd5807858546aeb1777991
  • Loading branch information
lattner authored and modularbot committed Dec 17, 2024
1 parent 829c2d8 commit d3fdfae
Show file tree
Hide file tree
Showing 98 changed files with 419 additions and 419 deletions.
2 changes: 1 addition & 1 deletion docs/manual/basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
" var first: Int\n",
" var second: Int\n",
"\n",
" fn __init__(inout self, first: Int, second: Int):\n",
" fn __init__(out self, first: Int, second: Int):\n",
" self.first = first\n",
" self.second = second\n",
"\n",
Expand Down
4 changes: 2 additions & 2 deletions docs/manual/decorators/nonmaterializable.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
"struct HasBool:\n",
" var x: Bool\n",
"\n",
" fn __init__(inout self, x: Bool):\n",
" fn __init__(out self, x: Bool):\n",
" self.x = x\n",
"\n",
" @always_inline(\"nodebug\")\n",
" fn __init__(inout self, nms: NmStruct):\n",
" fn __init__(out self, nms: NmStruct):\n",
" self.x = True if (nms.x == 77) else False\n",
"\n",
"@value\n",
Expand Down
4 changes: 2 additions & 2 deletions docs/manual/decorators/register-passable.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
" var a: Int\n",
" var b: Int\n",
"\n",
" fn __init__(inout self, one: Int, two: Int):\n",
" fn __init__(out self, one: Int, two: Int):\n",
" self.a = one\n",
" self.b = two\n",
"\n",
" fn __copyinit__(inout self, existing: Self):\n",
" fn __copyinit__(out self, existing: Self):\n",
" self.a = existing.a\n",
" self.b = existing.b\n",
"\n",
Expand Down
4 changes: 2 additions & 2 deletions docs/manual/decorators/staticmethod.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
"struct MyStruct:\n",
" var data: List[UInt8]\n",
"\n",
" fn __init__(inout self):\n",
" fn __init__(out self):\n",
" self.data = List[UInt8]()\n",
"\n",
" fn __moveinit__(inout self, owned existing: Self):\n",
" fn __moveinit__(out self, owned existing: Self):\n",
" self.data = existing.data ^\n",
"\n",
" @staticmethod\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/manual/decorators/value.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@
" var name: String\n",
" var age: Int\n",
"\n",
" fn __init__(inout self, owned name: String, age: Int):\n",
" fn __init__(out self, owned name: String, age: Int):\n",
" self.name = name^\n",
" self.age = age\n",
"\n",
" fn __copyinit__(inout self, existing: Self):\n",
" fn __copyinit__(out self, existing: Self):\n",
" self.name = existing.name\n",
" self.age = existing.age\n",
"\n",
" fn __moveinit__(inout self, owned existing: Self):\n",
" fn __moveinit__(out self, owned existing: Self):\n",
" self.name = existing.name^\n",
" self.age = existing.age"
]
Expand Down
2 changes: 1 addition & 1 deletion docs/manual/functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@
"source": [
"@value\n",
"struct MyString:\n",
" fn __init__(inout self, string: StringLiteral):\n",
" fn __init__(out self, string: StringLiteral):\n",
" pass\n",
"\n",
"fn foo(name: String):\n",
Expand Down
12 changes: 6 additions & 6 deletions docs/manual/lifecycle/death.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
" var name: String\n",
" var age: Int\n",
"\n",
" fn __init__(inout self, name: String, age: Int):\n",
" fn __init__(out self, name: String, age: Int):\n",
" self.name = name\n",
" self.age = age"
]
Expand Down Expand Up @@ -216,7 +216,7 @@
" var data: UnsafePointer[Int]\n",
" var size: Int\n",
"\n",
" fn __init__(inout self, size: Int, val: Int):\n",
" fn __init__(out self, size: Int, val: Int):\n",
" self.size = size\n",
" self.data = UnsafePointer[Int].alloc(self.size)\n",
" for i in range(self.size):\n",
Expand Down Expand Up @@ -272,7 +272,7 @@
" var name: String\n",
" var age: Int\n",
"\n",
" fn __init__(inout self, name: String, age: Int):\n",
" fn __init__(out self, name: String, age: Int):\n",
" self.name = name\n",
" self.age = age\n",
"\n",
Expand Down Expand Up @@ -398,7 +398,7 @@
"\n",
"```mojo\n",
"struct TwoStrings:\n",
" fn __moveinit__(inout self, owned existing: Self):\n",
" fn __moveinit__(out self, owned existing: Self):\n",
" # Initializes a new `self` by consuming the contents of `existing`\n",
" fn __del__(owned self):\n",
" # Destroys all resources in `self`\n",
Expand Down Expand Up @@ -448,11 +448,11 @@
" var str1: String\n",
" var str2: String\n",
"\n",
" fn __init__(inout self, one: String):\n",
" fn __init__(out self, one: String):\n",
" self.str1 = one\n",
" self.str2 = String(\"bar\")\n",
"\n",
" fn __moveinit__(inout self, owned existing: Self):\n",
" fn __moveinit__(out self, owned existing: Self):\n",
" self.str1 = existing.str1\n",
" self.str2 = existing.str2\n",
"\n",
Expand Down
44 changes: 22 additions & 22 deletions docs/manual/lifecycle/life.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
" var name: String\n",
" var age: Int\n",
"\n",
" fn __init__(inout self, name: String, age: Int):\n",
" fn __init__(out self, name: String, age: Int):\n",
" self.name = name\n",
" self.age = age"
]
Expand Down Expand Up @@ -181,11 +181,11 @@
" var name: String\n",
" var age: Int\n",
"\n",
" fn __init__(inout self):\n",
" fn __init__(out self):\n",
" self.name = \"\"\n",
" self.age = 0\n",
"\n",
" fn __init__(inout self, name: String):\n",
" fn __init__(out self, name: String):\n",
" self = MyPet()\n",
" self.name = name"
]
Expand Down Expand Up @@ -218,7 +218,7 @@
" var name: String\n",
" var age: Int\n",
"\n",
" fn __init__(inout self, name: String, age: Int, cond: Bool):\n",
" fn __init__(out self, name: String, age: Int, cond: Bool):\n",
" self.name = name\n",
" if cond:\n",
" self.age = age\n",
Expand Down Expand Up @@ -254,7 +254,7 @@
"\n",
"```mojo\n",
"struct Target:\n",
" fn __init__(inout self, s: Source): ...\n",
" fn __init__(out self, s: Source): ...\n",
"```\n",
"\n",
"With implicit conversion, the assignment above is essentially identical to:\n",
Expand All @@ -269,7 +269,7 @@
"\n",
"```mojo\n",
"struct Target:\n",
" fn __init__(inout self, s: Source, reverse: Bool = False): ...\n",
" fn __init__(out self, s: Source, reverse: Bool = False): ...\n",
"```\n",
"\n",
"Implicit conversion also occurs if the type doesn't declare its own constructor,\n",
Expand All @@ -293,14 +293,14 @@
"\n",
"```mojo\n",
"struct A: \n",
" fn __init__(inout self, s: Source): ...\n",
" fn __init__(out self, s: Source): ...\n",
"\n",
"struct B: \n",
" fn __init__(inout self, s: Source): ...\n",
" fn __init__(out self, s: Source): ...\n",
"\n",
"struct Target:\n",
" fn __init__(inout self, a: A): ...\n",
" fn __init__(inout self, b: B): ...\n",
" fn __init__(out self, a: A): ...\n",
" fn __init__(out self, b: B): ...\n",
"\n",
"# Fails\n",
"var t = Target(Source())\n",
Expand All @@ -316,7 +316,7 @@
"```mojo\n",
"struct Target:\n",
" # does not support implicit conversion\n",
" fn __init__(inout self, *, source: Source): ...\n",
" fn __init__(out self, *, source: Source): ...\n",
"\n",
"# the constructor must be called with a keyword\n",
"var t = Target(source=a)\n",
Expand Down Expand Up @@ -363,11 +363,11 @@
" var name: String\n",
" var age: Int\n",
"\n",
" fn __init__(inout self, name: String, age: Int):\n",
" fn __init__(out self, name: String, age: Int):\n",
" self.name = name\n",
" self.age = age\n",
"\n",
" fn __copyinit__(inout self, existing: Self):\n",
" fn __copyinit__(out self, existing: Self):\n",
" self.name = existing.name\n",
" self.age = existing.age"
]
Expand Down Expand Up @@ -433,14 +433,14 @@
" var size: Int\n",
" var cap: Int\n",
"\n",
" fn __init__(inout self, size: Int, val: Int):\n",
" fn __init__(out self, size: Int, val: Int):\n",
" self.size = size\n",
" self.cap = size * 2\n",
" self.data = UnsafePointer[Int].alloc(self.cap)\n",
" for i in range(self.size):\n",
" (self.data + i).init_pointee_copy(val)\n",
"\n",
" fn __copyinit__(inout self, existing: Self):\n",
" fn __copyinit__(out self, existing: Self):\n",
" # Deep-copy the existing value\n",
" self.size = existing.size\n",
" self.cap = existing.cap\n",
Expand Down Expand Up @@ -600,14 +600,14 @@
" var cap: Int\n",
"\n",
"\n",
" fn __init__(inout self, size: Int, val: Int):\n",
" fn __init__(out self, size: Int, val: Int):\n",
" self.size = size\n",
" self.cap = size * 2\n",
" self.data = UnsafePointer[Int].alloc(self.size)\n",
" for i in range(self.size):\n",
" (self.data + i).init_pointee_copy(val)\n",
"\n",
" fn __copyinit__(inout self, existing: Self):\n",
" fn __copyinit__(out self, existing: Self):\n",
" # Deep-copy the existing value\n",
" self.size = existing.size\n",
" self.cap = existing.cap\n",
Expand All @@ -616,7 +616,7 @@
" (self.data + i).init_pointee_copy(existing.data[i])\n",
" # The lifetime of `existing` continues unchanged\n",
"\n",
" fn __moveinit__(inout self, owned existing: Self):\n",
" fn __moveinit__(out self, owned existing: Self):\n",
" print(\"move\")\n",
" # Shallow copy the existing value\n",
" self.size = existing.size\n",
Expand Down Expand Up @@ -771,15 +771,15 @@
" var name: String\n",
" var age: Int\n",
"\n",
" fn __init__(inout self, owned name: String, age: Int):\n",
" fn __init__(out self, owned name: String, age: Int):\n",
" self.name = name^\n",
" self.age = age\n",
"\n",
" fn __copyinit__(inout self, existing: Self):\n",
" fn __copyinit__(out self, existing: Self):\n",
" self.name = existing.name\n",
" self.age = existing.age\n",
"\n",
" fn __moveinit__(inout self, owned existing: Self):\n",
" fn __moveinit__(out self, owned existing: Self):\n",
" self.name = existing.name^\n",
" self.age = existing.age"
]
Expand Down Expand Up @@ -809,7 +809,7 @@
" var name: String\n",
" var age: Int\n",
"\n",
" fn __init__(inout self, owned name: String):\n",
" fn __init__(out self, owned name: String):\n",
" self.name = name^\n",
" self.age = 0\n"
]
Expand Down
16 changes: 8 additions & 8 deletions docs/manual/parameters/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
" var data: UnsafePointer[ElementType]\n",
" var size: Int\n",
"\n",
" fn __init__(inout self, *elements: ElementType):\n",
" fn __init__(out self, *elements: ElementType):\n",
" self.size = len(elements)\n",
" self.data = UnsafePointer[ElementType].alloc(self.size)\n",
" for i in range(self.size):\n",
Expand Down Expand Up @@ -438,7 +438,7 @@
" var value: … # Some low-level MLIR stuff here\n",
"\n",
" # Create a new SIMD from a number of scalars\n",
" fn __init__(inout self, *elems: SIMD[type, 1]): ...\n",
" fn __init__(out self, *elems: SIMD[type, 1]): ...\n",
"\n",
" # Fill a SIMD with a duplicated scalar value.\n",
" @staticmethod\n",
Expand Down Expand Up @@ -536,7 +536,7 @@
" var value: Int\n",
"\n",
" @always_inline(\"nodebug\")\n",
" fn __init__(inout self, _a: Int):\n",
" fn __init__(out self, _a: Int):\n",
" self.value = _a\n",
"\n",
"fn foo[x: MyInt, a: Int]():\n",
Expand Down Expand Up @@ -569,7 +569,7 @@
"parameter_overloads[1, 2, MyInt(3)]()\n",
"\n",
"struct MyStruct:\n",
" fn __init__(inout self):\n",
" fn __init__(out self):\n",
" pass\n",
"\n",
" fn foo(inout self):\n",
Expand Down Expand Up @@ -730,7 +730,7 @@
"struct One[Type: StringableCollectionElement]:\n",
" var value: Type\n",
"\n",
" fn __init__(inout self, value: Type):\n",
" fn __init__(out self, value: Type):\n",
" self.value = value\n",
"\n",
"def use_one():\n",
Expand Down Expand Up @@ -768,7 +768,7 @@
" var val1: Type\n",
" var val2: Type\n",
"\n",
" fn __init__(inout self, one: One[Type], another: One[Type]):\n",
" fn __init__(out self, one: One[Type], another: One[Type]):\n",
" self.val1 = one.value\n",
" self.val2 = another.value\n",
" print(str(self.val1), str(self.val2))\n",
Expand Down Expand Up @@ -886,7 +886,7 @@
"outputs": [],
"source": [
"struct KwParamStruct[greeting: String = \"Hello\", name: String = \"🔥mojo🔥\"]:\n",
" fn __init__(inout self):\n",
" fn __init__(out self):\n",
" print(greeting, name)\n",
"\n",
"fn use_kw_params():\n",
Expand Down Expand Up @@ -1193,7 +1193,7 @@
" var data: UnsafePointer[T]\n",
" var size: Int\n",
"\n",
" fn __init__(inout self, size: Int, value: T):\n",
" fn __init__(out self, size: Int, value: T):\n",
" self.size = size\n",
" self.data = UnsafePointer[T].alloc(self.size)\n",
" for i in range(self.size):\n",
Expand Down
Loading

0 comments on commit d3fdfae

Please sign in to comment.