proxy

f<Type>(arguments)

moon add Yoorkin/proxy@0.1.0
Download zip
Author
Version
0.1.0
License
Apache-2.0
Last updated
2 months ago
Downloads
19
README

#proxy

proxy is a library for MoonBit users who want to manually specify a type parameter in a function call, similar to f<Type>(args) in some other languages.

#You might not need this

Before using Proxy[T], keep this in mind: you usually do not need it. Syntax like f<Type>(args) is often used in languages that cannot infer generic parameters, but MoonBit can infer them in most cases. Let's go through those cases one by one.

  • Case 0: the function parameter uses a generic type

    Just pass the argument directly, and let MoonBit infer the type:

    fn[T : Show] to_string(x : T) -> String {
    Show::to_string(x)
    }

    test {
    let a = to_string(1)
    let b = to_string(false)
    assert_eq(a, "1")
    assert_eq(b, "false")
    }

  • Case 1: the function returns the generic type

    Type inference is still available when you pass the result directly to another function, as long as that function has enough type information.

    fn[T : @string.FromStr] from_string(x : String) -> T {
    try! @string.from_str(x)
    }

    fn f(_ : Int, _ : Bool) -> Unit {
    ...
    }

    test {
    let a = from_string("123")
    let b = from_string("false")
    f(a, b)
    }

    If inference is not available, add a type annotation to a let binding or expression with : Type.

    test {
    let a : Int = from_string("123")
    let b = (from_string("false") : Bool)
    assert_eq(a, 123)
    assert_eq(b, false)
    }

  • Case 2: the function returns a type that contains the generic type

    You can still rely on type inference. If inference is not available, add a partial type annotation and use _ for the type arguments you do not care about.

    fn[T : @string.FromStr] try_from_string(x : String) -> Result[T, Error] {
    try! @string.from_str(x)
    }

    ///|
    #warnings("-partial_match")
    test {
    let Ok(a) : Result[Int, _] = try_from_string("123")
    let Ok((b : Bool)) = try_from_string("false")
    assert_eq(a, 123)
    assert_eq(b, false)
    }

  • Case 3: neither the parameters nor the return value use the generic type

    This is the rare case where type inference is completely impossible. Use this library to provide the missing type information.

    ///|
    trait TypeInfo {
    fn name(@proxy.Proxy[Self]) -> String
    }

    ///|
    impl TypeInfo for Int with fn name(_) {
    "Int"
    }

    ///|
    impl TypeInfo for Bool with fn name(_) {
    "Bool"
    }

    ///|
    test {
    let a = TypeInfo::name((Proxy : @proxy.Proxy[Int]))
    let b = TypeInfo::name((Proxy : @proxy.Proxy[Bool]))
    assert_eq(a, "Int")
    assert_eq(b, "Bool")
    }

    The trick is simple: pass the dummy value Proxy, which has a generic type, and wrap it in a type annotation such as (expr : Proxy[Int]).

#
Proxy

pub(all) enum Proxy[T] {
Proxy
}

Source Files

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io