Search Mailing List Archives


Limit search to: Subject & Body Subject Author
Sort by: Reverse Sort
Limit to: All This Week Last Week This Month Last Month
Select Date Range     through    

[terralang] How to defer the typing of a variable?

Zach DeVito zdevito at stanford.edu
Mon Jun 22 12:43:37 PDT 2015


Macros are run during typechecking allowing you to query the types of
arguments. So you can do something like:

local lazyselect = macro(function(c,x,y)
    local T = x:gettype()
    return quote
        var r : T
        if c then
            r = x
        else
            r = y
        end
    in
        r
    end
end)

terra foo(a : bool, b : int, c : int)
    return lazyselect(a,b,c)
end

print(foo(false,1,2))
print(foo(true,1,2))


On Mon, Jun 22, 2015 at 6:26 AM, Leonard Ritter <paniq at paniq.org> wrote:

> Hi everyone,
>
> I'm writing a code generator that works based on expressions only (very
> much in the spirit of Lisp and Scheme), and I hit upon a problem today
> where terralib.select would not include the statements included in a quoted
> expression in its branch but rather post them top level.
>
> I filed a bug report for that here
> https://github.com/zdevito/terra/issues/99
>
> So instead, I went and looked into how to do conditional expressions with
> if-else blocks instead, and it seems I can not purely do this with
> expressions. What I had in mind was something like this:
>
> quote
>     var result
>     if [ condition ] then
>         result = [true_expr]
>     else
>         result = [false_expr]
>     end
> in
>     result
> end
> ( https://gist.github.com/paniq/a6909d6edc26e4123d7e )
>
> however, that approach doesn't work because result needs to be typed if it
> lacks an initializer -- but
> due to separation of interest, I can't know the type until the first
> assignment.
>
> It seems without this feature, I can't generate static code from an
> S-expression based syntax tree.
>
> My question is therefore: what are my options in this situation? Can I
> defer the typing of a variable until I have an expression I can assign, if
> yes, how?
>
> Thank you for your help,
>
> Leonard
>
> _______________________________________________
> terralang mailing list
> terralang at lists.stanford.edu
> https://mailman.stanford.edu/mailman/listinfo/terralang
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.stanford.edu/pipermail/terralang/attachments/20150622/0b653fe2/attachment.html>


More information about the terralang mailing list