Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for union types by the language? #13

Closed
PavelVozenilek opened this issue Nov 12, 2016 · 4 comments
Closed

Support for union types by the language? #13

PavelVozenilek opened this issue Nov 12, 2016 · 4 comments

Comments

@PavelVozenilek
Copy link

Say I have:

type Char =  c8|c32 c
let Char ch = ...

Does the language support "polymorphic use" through union parameters? Say I have functions:

fun foo: ch8 par = ...
fun foo: c32 par = ...

// Now the compiler may insert run-time decision 
// which function to call. 
// It could also do compile-time check that all possibilities are covered, 
// that there's a foo overload for every union member
//
foo(ch)  // will this work?
@jfecher
Copy link
Owner

jfecher commented Nov 12, 2016

That will not work, if you are interested in something like virtual functions there are plans to include them, but I haven't decided on syntax or anything on them yet.

Going back to your example though, it would be possible for the compiler to say expand foo ch to

match ch with
| c8 c -> foo c
| c32 c -> foo c

Which would probably be a tad slower than virtual functions but you wouldn't have to store the function in the datatype. This would honestly be low on my list of priorities for the 1.0 release as you could just write a macro function to do the same thing:

![macro]
fun union_call: ('t->'u) fn, 't arg
    let argty = typeof arg
    if not isUnionTy argty then
        compErr "Argument to union_call macro must be a union type!" (callsite())

    var baseStr = "match ${Node arg} with\n"

    //this loop should always run at least twice because all unions have >= 2 members
    for ty in getUnionMemberTys argty do
        baseStr ++= "| ${Node ty} u -> ${Node fn} ${Node arg}\n"

    callsite().insert <| parse baseStr

then just call it with union_call foo ch and it even does error checking

@PavelVozenilek
Copy link
Author

Such feature would probably have different implementation than ordinary vtable, it may be more like Smalltalk does it.

Anyway, I understood the macro example, really good one.


Will it be possible to invoke a macro without explicitly using its name (the union_call here), e.g. to hijack some existing function? For example to wrap some function(s) with mutex, w/o touching the source or how they are used in an application?

@jfecher
Copy link
Owner

jfecher commented Nov 12, 2016

To do that you would probably have to add that macro function as an event listener for a function/variable not found event

Ante.events.onFnNotFound ++= 
    fun f args =
        if len args < 1 then return ()

        if isUnion (args#0) then
            union_call f args

//or
![onEvent FnNotFound]
fun f args =
    if len args < 1 then return ()

    if isUnion (args#0) then
        union_call f args

@PavelVozenilek
Copy link
Author

Ouch, this is still over my head.

It would be very handy to replace certain existing functions, even better if only inside specific scope. E.g. inside unit tests, instead of tricks or design changes to support mocking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants