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 Pragmatic 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
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |&gt; handle_event() |&gt; render() but the correc...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
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
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
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
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New

Other popular topics Top

wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
Exadra37
I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
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
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New

Latest in PragProg

View all threads ❯