Comments (4)
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
from ante.
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?
from ante.
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
from ante.
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.
from ante.
Related Issues (20)
- Improve Location for "type variable is already in scope" error
- Codegen crash when using a trait that defines multiple methods
- What kind of memory management does ante use? HOT 3
- Implement Named Constructor syntax HOT 2
- Adding type annotations to an unbound type variable can crash the compiler HOT 1
- Implement `can Effect` syntax for annotating effects HOT 1
- Check declared variable's types with equality instead of unification HOT 1
- Add a proper error message for unhandled effects
- Incorrect Pointer Offset Calculation When Using -O3 Optimization Level HOT 1
- Add shorthand for casting integer types
- Malloc Panics with -O0 or crane_lift HOT 1
- Require the package name to prefix all imports
- Add `./deps` as a relative root by default
- Add a command-line flag to configure the "relative_root" directories
- Create a LSP server for Ante HOT 4
- WebAssembly HOT 2
- Values declared inside conditional blocks and match arms leak to module export list HOT 2
- Monomorphisation panic HOT 3
- Implement effect variable ellision
- Panic: `Recursion limit reached in occurs`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ante.