-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/nightly' into move-strref-find…
…-impl-to-span
- Loading branch information
Showing
86 changed files
with
11,188 additions
and
9,721 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
title: '@implicit' | ||
description: Marks a constructor as eligible for implicit conversion. | ||
codeTitle: true | ||
|
||
--- | ||
|
||
You can add the `@implicit` decorator on any single-argument constructor to | ||
identify it as eligible for implicit conversion. | ||
|
||
For example: | ||
|
||
```mojo | ||
struct MyInt: | ||
var value: Int | ||
@implicit | ||
fn __init__(out self, value: Int): | ||
self.value = value | ||
fn __init__(out self, value: Float64): | ||
self.value = int(value) | ||
``` | ||
|
||
This implicit conversion constructor allows you to pass an `Int` to a function | ||
that takes a `MyInt` argument, or assign an `Int` to a variable of type `MyInt`. | ||
However, the constructor that takes a `Float64` value is **not** an implicit | ||
conversion constructor, so it must be invoked explicitly: | ||
|
||
```mojo | ||
fn func(n: MyInt): | ||
print("MyInt value: ", n.value) | ||
fn main(): | ||
func(Int(42)) # Implicit conversion from Int: OK | ||
func(MyInt(Float64(4.2))) # Explicit conversion from Float64: OK | ||
func(Float64(4.2)) # Error: can't convert Float64 to MyInt | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.