Skip to content

Instantly share code, notes, and snippets.

@trecouvr
Created December 21, 2011 18:36
Show Gist options
  • Save trecouvr/1507131 to your computer and use it in GitHub Desktop.
Save trecouvr/1507131 to your computer and use it in GitHub Desktop.
opa subtyping emulation
type AAA = {AAA : string}
type AAB = {AAB : string}
type ABA = {ABA : string}
type ABB = {ABB : string}
type AA = {AA : string fils : AAA/AAB/{none}}
type AB = {AB : string fils : ABA/ABB/{none}}
type A = {a1 : string a2 : string fils : AA/AB/{none}}
print_a1(v : A) =
jlog(v.a1)
typeof(v : A) : string =
match v.fils with
| {none} -> "A"
| {~AA ~fils} ->
match fils with
| {none} -> "AA"
| {~AAA} -> "AAA"
| {~AAB} -> "AAB"
end
| {~AB ~fils} ->
match fils with
| {none} -> "AB"
| {~ABA} -> "ABA"
| {~ABB} -> "ABB"
end
end
db /mydb : map(string,A)
db /mydb[_]/fils={none}
a = {a1="a" a2="" fils={none}}
aa = {a1="a" a2="" fils={AA="" fils={none}}}
aaa = {a1="a" a2="" fils={AA="" fils={AAA=""}}}
type A = {a1 : string a2 : string}
type AA = {a1 : string a2 : string AA : string}
type AAA = {a1 : string a2 : string AA : string AAA : string}
type AAB = {a1 : string a2 : string AA : string AAB : string}
type AB = {a1 : string a2 : string AB : string}
type ABA = {a1 : string a2 : string AB : string ABA : string}
type ABB = {a1 : string a2 : string AB : string ABB : string}
type All = A / AA / AAA / AAB / AB / ABA / ABB
print_a1(v : {a1 : string a2 : string ...}) =
jlog(v.a1)
typeof(v : All) : string =
match v with
| {~AAA ~AA ~a1 ~a2} -> "AAA"
| {~AAB ~AA ~a1 ~a2} -> "AAB"
| {~ABA ~AB ~a1 ~a2} -> "ABA"
| {~ABB ~AB ~a1 ~a2} -> "ABB"
| {~AA ~a1 ~a2} -> "AA"
| {~AB ~a1 ~a2} -> "AB"
| {~a1 ~a2} -> "A"
end
db /mydb : map(string,All)
db /mydb[_] = {a1="" a2=""}
a = {a1="a" a2=""}
aa = {a1="a" a2="" AA=""}
aaa = {a1="a" a2="" AA="" AAA=""}
do print_a1(a)
do print_a1(aa)
do print_a1(aaa)
do jlog(typeof(a))
do jlog(typeof(aa))
do jlog(typeof(aaa))
main() =
<div>Hello</div>
server = Server.one_page_server("test", main)
type A = {a1 : string a2 : string}
type AA = {A with AA : string}
type AAA = {AA with AAA : string}
type AB = {A with AB : string}
type ABA = {AB with ABA : string}
type ABB = {AB with ABB : string}
print_a1(v : A) : string = do jlog(v.a1)
typeof(v : A) : string =
match v with
| record(AAA) -> "AAA"
| record(AAB) -> "AAB"
| record(AA) -> "AA"
| record(ABA) -> "ABA"
| record(ABB) -> "ABB"
| record(AB) -> "AB"
| record(A) -> "A"
end
a = {a1="a" a2=""}
aa = {a1="a" a2="" AA=""}
aaa = {a1="a" a2="" AA="" AAA=""}
do print_a1(a)
do print_a1(aa)
do print_a1(aaa)
db /mydb : map(string, A)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment