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.

Popular Prag Prog topics Top

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
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
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
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
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New
dtonhofer
@parrt In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New

Other popular topics Top

malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
AstonJ
Chris Seaton, the creator of TruffleRuby has died. It appears from suicide :cry: He left this note on Twitter on the weekend: And one...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New
CommunityNews
A Brief Review of the Minisforum V3 AMD Tablet. Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New

Latest in PragProg

View all threads ❯