novitk

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

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

Dead link:
https://github.com/scala-native/scala-native/blob/master/nativelib/src/main/scala/scala/scalanative/native/stdlib.scala

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.

Where Next?

Popular Pragmatic Bookshelf topics Top

brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
johnp
Hi Brian, Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
adamwoolhether
I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
taguniversalmachine
Hi, I am getting an error I cannot figure out on my test. I have what I think is the exact code from the book, other than I changed “us...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New

Other popular topics Top

DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
New
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New

Sub Categories: