-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Continue Updating Claro Docs to Use Auto-Validated Examples
- Loading branch information
1 parent
a0a20d9
commit bb67a3d
Showing
14 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
load("//mdbook_docs:docs_with_validated_examples.bzl", "doc_with_validated_examples") | ||
|
||
doc_with_validated_examples( | ||
name = "swapping_deps", | ||
doc_template = "swapping_deps.tmpl.md", | ||
examples = [], | ||
) |
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,84 @@ | ||
load("//:rules.bzl", "claro_binary", "claro_module") | ||
load("//mdbook_docs:docs_with_validated_examples.bzl", "doc_with_validated_examples") | ||
|
||
doc_with_validated_examples( | ||
name = "swapping_deps", | ||
doc_template = "swapping_deps.tmpl.md", | ||
examples = [ | ||
{ | ||
"example": "get_message.claro_module_api", | ||
"executable": False, | ||
"codeblock_css_class": "claro", | ||
}, | ||
{ | ||
"example": "modules_BULD", | ||
"executable": False, | ||
"codeblock_css_class": "python", | ||
}, | ||
{ | ||
"example": "example.claro", | ||
"append_output": False, | ||
"deps": {"Msg": ":look_ma"}, | ||
}, | ||
{ | ||
"example": "binary_hello_BUILD", | ||
"executable": False, | ||
"codeblock_css_class": "python", | ||
}, | ||
{ | ||
"example": ":hello_out", | ||
"executable": False, | ||
}, | ||
{ | ||
"example": "binary_look_ma_BUILD", | ||
"executable": False, | ||
"codeblock_css_class": "python", | ||
}, | ||
{ | ||
"example": ":look_ma_out", | ||
"executable": False, | ||
}, | ||
{ | ||
"example": "extra_exports.claro_module_api", | ||
"executable": False, | ||
"codeblock_css_class": "claro", | ||
}, | ||
], | ||
) | ||
|
||
claro_binary( | ||
name = "test_hello", | ||
main_file = "example.claro", | ||
deps = { | ||
"Msg": ":hello_world", | ||
} | ||
) | ||
claro_binary( | ||
name = "test_look_ma", | ||
main_file = "example.claro", | ||
deps = { | ||
"Msg": ":look_ma", | ||
} | ||
) | ||
claro_module( | ||
name = "hello_world", | ||
module_api_file = "get_message.claro_module_api", | ||
srcs = ["hello_world.claro"], | ||
) | ||
claro_module( | ||
name = "look_ma", | ||
module_api_file = "get_message.claro_module_api", | ||
srcs = ["look_ma.claro"], | ||
) | ||
|
||
[ | ||
genrule( | ||
name = name + "_out", | ||
outs = [name + "_out.txt"], | ||
srcs = ["test_" + name + "_deploy.jar"], | ||
cmd = "$(JAVA) -jar $(SRCS) > $(OUTS)", | ||
tools = ["@bazel_tools//tools/jdk:current_java_runtime"], | ||
toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"], | ||
) | ||
for name in ["hello", "look_ma"] | ||
] |
8 changes: 8 additions & 0 deletions
8
mdbook_docs/src/module_system/swapping_deps/binary_hello_BUILD
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,8 @@ | ||
# BUILD | ||
claro_binary( | ||
name = "test", | ||
main_file = "example.claro", | ||
deps = { | ||
"Msg": ":hello_world", | ||
} | ||
) |
8 changes: 8 additions & 0 deletions
8
mdbook_docs/src/module_system/swapping_deps/binary_look_ma_BUILD
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,8 @@ | ||
# BUILD | ||
claro_binary( | ||
name = "test", | ||
main_file = "example.claro", | ||
deps = { | ||
"Msg": ":look_ma", # Swapped for `:hello_world`. | ||
} | ||
) |
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,2 @@ | ||
# example.claro | ||
print(Msg::getMessage()); |
8 changes: 8 additions & 0 deletions
8
mdbook_docs/src/module_system/swapping_deps/extra_exports.claro_module_api
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,8 @@ | ||
# extra_exports.claro_module_api | ||
provider getMessage() -> string; | ||
|
||
provider getMessageFromDB() -> future<string>; | ||
|
||
opaque newtype SecretMessage | ||
|
||
static SOME_OTHER_MESSAGE : SecretMessage; |
2 changes: 2 additions & 0 deletions
2
mdbook_docs/src/module_system/swapping_deps/get_message.claro_module_api
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,2 @@ | ||
# get_message.claro_module_api | ||
provider getMessage() -> string; |
3 changes: 3 additions & 0 deletions
3
mdbook_docs/src/module_system/swapping_deps/hello_world.claro
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,3 @@ | ||
provider getMessage() -> string { | ||
return "Hello, World!"; | ||
} |
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,5 @@ | ||
provider getMessage() -> string { | ||
var msg = "Look ma, no hands!"; | ||
var line = strings::repeated("-", len(msg) + 4); | ||
return "{line}\n| {msg} |\n{line}"; | ||
} |
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,14 @@ | ||
# BUILD | ||
load("//:rules.bzl", "claro_binary", "claro_module") | ||
|
||
claro_module( | ||
name = "hello_world", | ||
module_api_file = "get_message.claro_module_api", | ||
srcs = ["hello_world.claro"], | ||
) | ||
claro_module( | ||
name = "look_ma", | ||
module_api_file = "get_message.claro_module_api", | ||
srcs = ["look_ma.claro"], | ||
) | ||
# ... |
55 changes: 55 additions & 0 deletions
55
mdbook_docs/src/module_system/swapping_deps/swapping_deps.tmpl.md
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,55 @@ | ||
# Swapping Dependencies | ||
|
||
Claro's Module system was very carefully designed to guarantee that it's **statically impossible for two separate | ||
modules to be | ||
<a href="https://www.wikiwand.com/en/Coupling_(computer_programming)#introduction" target="_blank">"tightly coupled"</a>**. | ||
In this section we'll dive into exactly what that means. | ||
|
||
As you've already seen in previous sections, Claro Modules explicitly declare a public API that indicates the full set | ||
of procedures/values/Types that the Module's consumers will gain access to. Of course, some form of this is present in | ||
every language. The unique distinction is that Claro Module dependencies can be directly swapped out to any other Module | ||
with an appropriate API **without changing a single line of code in any `.claro` source files**. | ||
|
||
For example, the below API... | ||
|
||
{{EX1}} | ||
|
||
...could be implemented by multiple Modules... | ||
|
||
{{EX2}} | ||
|
||
...and then the **exact same `.claro` source code**... | ||
|
||
{{EX3}} | ||
|
||
...could be compiled against either Module... | ||
|
||
{{EX4}} | ||
{{EX5}} | ||
|
||
...and the behavior would depend on which dependency was chosen... | ||
|
||
{{EX6}} | ||
{{EX7}} | ||
|
||
## Dep Validity is Based on Usage | ||
|
||
The other subtle point that's likely easy to miss if it's not pointed out explicitly is that the validity of a Module | ||
dependency is completely dependent upon the _usage_ of the dependency. In less opaque terms, this just means that a | ||
Module dependency is valid if the Module's API actually exports everything that is _used_ by the consuming code. The | ||
consuming code doesn't make any constraints on _anything other than what it actually uses_. So, a dependency can be | ||
swapped for another that actually exports a _completely different_ API, so long as it **_at least_** exports everything | ||
that the consuming code _actually used_ from the original Module's API. | ||
|
||
For example, if a third Module actually implemented a totally different API such as: | ||
|
||
{{EX8}} | ||
|
||
the dependency would _still_ be valid because `example.claro` only actually _uses_ the `getMessage(...)` procedure that | ||
is exported by both `:look_ma` and `:hello_world`. | ||
|
||
This single design decision actually enables a huge amount of Build time configurability options. If you'd like to see | ||
more about how you can take advantage of this, read about how you can swap dependencies programmatically using | ||
[Build flags](../../metaprogramming/swapping_deps/swapping_deps.generated_docs.md). And if you're interested in becoming | ||
a power-user, this feature can be leveraged in some very powerful ways via | ||
[Build Time Metaprogramming](../../metaprogramming/metaprogramming.generated_docs.md). |