Skip to content

Instantly share code, notes, and snippets.

@Groostav
Last active March 1, 2023 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Groostav/68234f5ab6d5582b182990c16b548db8 to your computer and use it in GitHub Desktop.
Save Groostav/68234f5ab6d5582b182990c16b548db8 to your computer and use it in GitHub Desktop.
An attempt to create a statically-checked "domain-path" type
sealed interface PathTrace<out T> {
val elements: List<Nodable>
class Composite(override val elements: List<Nodable>): PathTrace<Nothing>
open class Empty: PathTrace<Nothing> {
override val elements: List<Nodable> get() = emptyList()
}
}
sealed interface Nodable
interface Branch<T>: Nodable
interface Leaf: Nodable
object Root: PathTrace.Empty(), Branch<Root.Component> {
sealed interface Component: Nodable
}
object Model: Root.Component, Branch<Model.Component> {
sealed interface Component: Nodable
}
object Run: Root.Component, Branch<Run.Component> {
sealed interface Component: Nodable
}
// children of Model
object Inputs: Model.Component, Leaf
object Outputs: Model.Component, Leaf
object Simulations: Model.Component, Branch<Simulations.Component> {
sealed interface Component: Nodable
}
// children of Simulations
data class Proxy(val invocationName: String): Simulations.Component, Branch<Proxy.Component> {
sealed interface Component: Nodable
}
// children of Proxy
object InputFile: Proxy.Component, Leaf
operator fun <TChild: Nodable, R: TChild> PathTrace<Branch<TChild>>.div(next: R): PathTrace<R> {
return when(this){
is PathTrace.Composite -> PathTrace.Composite(elements + next)
is PathTrace.Empty -> PathTrace.Composite(listOf(next))
}
}
fun usage(){
val path = Root / Model / Simulations / Proxy("asdf") / InputFile
// some actual uses
eventingSystem.onHighlightRequest { path ->
if(path.elements startsWith Root / Model / Simualtions){
flourish(myUINodes)
}
}
userClicksGoToSimulationsHyperlink { clickEvent ->
eventingSystem.highlightUIFor(Root / Model / Simulations)
}
}
//i think if im willing to give up my slash syntax, i can do
// Root.Model.Simulations.Proxy("asdf").InputFile
// fairly trivially, with vastly simpler code. :\
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment