Code Monkey home page Code Monkey logo

Comments (6)

jfecher avatar jfecher commented on August 19, 2024

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.

ivanbakel avatar ivanbakel commented on August 19, 2024

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.

jfecher avatar jfecher commented on August 19, 2024

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.

ivanbakel avatar ivanbakel commented on August 19, 2024

Shouldn't printf consider Str a single type instead of auto-expanding the tuple?

from ante.

jfecher avatar jfecher commented on August 19, 2024

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.

jfecher avatar jfecher commented on August 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.