Comments (6)
Can I see your code? I am using the following and am getting the correct output of 1 and Test:
type MaybeIntStr =
| Some u8, Str
| None
fun printmis: MaybeIntStr m
match m with
| Some t -> printf "%d and %s\n" (t#0) (t#1); ()
| None -> print "None"
let s = Some(1u8, "Test")
printmis s
The ; ()
after the printf is just to satisfy the typecheck of the match branches so they all return void
from ante.
The key difference is that I did "%s and %d\n" (t#1) (t#0)
instead. This looks like the %d
is taking from the Str
the first time. "%s and %d and %d\n"
prints the length followed by t#0
.
from ante.
printf expects a c-string, and the Str datatype is a c-string and its length. Instead of t#1
use t#1#0
or the equivalent (t#1).cStr
from ante.
Shouldn't printf consider Str
a single type instead of auto-expanding the tuple?
from ante.
Str is a tuple type, it does not need to be auto expanded. Ante passes arguments by value by default so it passes the entire (c8*, u32) that is a Str. There is no auto-expanding step, the address a tuple is stored at is the same as the address of its first member type, theres no pointer indirection or anything. The odd behaviour is caused by the lack of type safety C's printf function provides, without specific compiler support it has no idea you mistakenly passed a tuple to it instead of a pointer.
from ante.
After writing all that out it occurred to me that you probably meant to ask why the implementation details of printf cause it to take the u32 len of the Str as the %d term instead of the passed u8. This is most likely because printf uses va_arg to increment to the next parameter. va_arg does this by taking the the type in question and increments it by the size of the type. Since printf thinks the Str is just a c8*, it only increments it by that amount and ends up on the second member of the tuple instead of the u8 following it. This is all because varargs are contiguous in memory.
from ante.
Related Issues (20)
- 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`
- Implement "Go to Declaration" for Ante-LS
- Mutable variables alias instead of copy
- Remove unneeded parenthesis from type printing
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.