| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
Mischief.Assets.Tutorial.Refs
Synopsis
Chapter 2: Asset Refs
When you want to point to an asset, you use an . This is a refernece specifying how to get to the asset you're looking for. It
could be a simple loaded asset, an asset referenced in another asset, or even a subasset (a part of a larger asset).AssetRef
Asset Querying
An AssetRef a can be used with functions such as and asset to attempt to grab the qasseta the ref is pointing to.
Assume you have these two .kdl assets:
assets/foo.kdl
foo {
f 10
bar "bars/bar.kdl"
}
assets/bars/bar.kdl
bar {
b 15
}
And their represenatations in Haskell:
data Foo = Foo {
f :: Int,
bar :: AssetRef Bar
}
data Bar = Bar {
b :: Int
}
To query for the Foo asset, you need to provide an AssetRef Foo, which can be the path to the asset:
foo :: Foo <- asset "foo.kdl"
You can also query directly for the f field of a Foo:
f <- asset $ do
foo :: Foo <- "foo.kdl"
pure foo.f
And you can query for the Bar asset referenced by a certain Foo:
bar <- asset $ do
foo :: Foo <- "foo.kdl"
foo.bar
Note that this is equivalent to referencing Bar's file directly, assuming you know the path to it:
bar <- asset $ "bars/bar.kdl"
Kdl Refs
For kdl files in particular, there is an utility called a KdlRef.
Let's rewrite the earlier types, this time with KdlRefs instead of AssetRefs:
data Foo = Foo {
f :: Int
bar :: KdlRef Bar
}
data Bar = Bar {
b :: Int
}
A KdlRef can have two possible values: either a reference to another file, or directly the value of the other asset:
So both of these are valid:
-- foo.kdl
foo {
f 10
bar link="bar.kdl"
}
-- bar.kdl
bar {
b 15
}
-- foo.kdl
foo {
f 10
bar {
b 15 s
}
}
You can convert a KdlRef to an AssetRef using toRef. For instance, this is how we'd get the value of b from a Foo file:
b <-asset$ do foo :: Foo <- "foo.kdl" bar <-toReffoo.bar pure bar.b