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 Bookshelf topics Top

jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
New
kuroneko
Whilst the author has been careful to provide exact results for the tests elsewhere in the book (such as surds with the transformation te...
New
iPaul
page 37 ANTLRInputStream input = new ANTLRInputStream(is); as of ANTLR 4 .8 should be: CharStream stream = CharStreams.fromStream(i...
New
johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
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
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
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

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
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
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
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
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
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
sir.laksmana_wenk
I’m able to do the “artistic” part of game-development; character designing/modeling, music, environment modeling, etc. However, I don’t...
New

Sub Categories: