
novitk
Modern Systems Programming with Scala Native - Printf wrapper doesn't look to be in v0.4.0-M2
the subj.
novitk@DESKTOP-KN:~/repos/scala-native$ grep -m 1 -B 10 printf clib/src/main/scala/scala/scalanative/libc/package.scala
package scala.scalanative
import scalanative.unsafe._
import scalanative.libc.stdio
package object libc {
implicit class StdioHelpers(val _stdio: libc.stdio.type) extends AnyVal {
def printf(format: CString, args: CVarArg*): CInt =
novitk@DESKTOP-KN:~/repos/scala-native$ diff <(git ls-tree --name-only -r master clib) <(git ls-tree --name-only -r tags/v0.4.0-M2 clib)
6d5
< clib/src/main/scala/scala/scalanative/libc/package.scala
Most Liked

neozenith
I’m running into the same issue regarding printf
if I follow writing out the code as it is shown in the book.
I tried looking up the official documentation to see where printf
is defined and the docs have dead links.
https://scala-native.readthedocs.io/en/v0.3.9-docs/lib/libc.html
Real link:
So nativelib
was renamed to clib
. So to be fair, scala-native
is v0.3.9 and not stabilised yet so these quirks are part of the territory for bleeding edge.
Circling back to the pragprog listing though, I downloaded the latest source code which adds a compat.scala
file which remaps printf
to vprintf
.
package scala.scalanative
import scalanative.unsafe._
import scalanative.libc.stdio
package object libc {
implicit class StdioHelpers(val _stdio: libc.stdio.type) extends AnyVal {
def printf(format: CString, args: CVarArg*): CInt =
Zone { implicit z =>
stdio.vprintf(format, toCVarArgList(args.toSeq))
}
// Other unrelated code truncated
}
}
If I download the source from:
http://media.pragprog.com/titles/rwscala/code/rwscala-code.zip
Extract it.
Then cd
into code/InputAndOutput/hello_native
and run sbt run
it works.
Also I thought it was a typo between the “hello” and “hello_native” projects but it seems like capitalising the object name Main
is important when linking.
hello/hello.scala
object main {
def main(args:Array[String]) {
println("hello, world!")
}
}
hello_native/hello.scala
import scala.scalanative.unsafe._
import scala.scalanative.libc._
object Main {
def main(args: Array[String]): Unit = {
stdio.printf(c"Hello native %s!\n", c"world")
}
}
After some digging it seems you can use vprintf
directly using the following snippet:
import scala.scalanative.unsafe._
import scala.scalanative.libc._
object Main {
def main(args: Array[String]) {
Zone { implicit z =>
stdio.vprintf(c"Hello native %s!\n", toCVarArgList(c"world"))
}
}
}
Using vprintf
gave me the error “Given method requires an implicit zone”.
https://scala-native.readthedocs.io/en/v0.3.9-docs/user/interop.html#memory-management
Adding the Zone { implicit z => ..... }
creates a section of code to help with unmanaged memory allocations like our CVarArgList.
Popular Prag Prog topics










Other popular topics










Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /scala
- /zig
- /html
- /debian
- /nixos
- /lisp
- /agda
- /textmate
- /sublime-text
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /manjaro
- /django
- /spring
- /diversity
- /lua
- /nodejs
- /julia
- /c
- /slackware
- /neovim