Skip to content

Commit

Permalink
Fix Scala Native Example Code (#3899)
Browse files Browse the repository at this point in the history
Pull request:  #3899
  • Loading branch information
c0d33ngr authored Nov 4, 2024
1 parent c1f4793 commit 96c4057
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 14 deletions.
5 changes: 2 additions & 3 deletions example/scalalib/native/1-simple/src/Foo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import mainargs.{main, ParserForMethods}

object Foo {

def generateHtml(text: String): CString = {
def generateHtml(text: String) (using Zone) = {
val html = "<h1>" + text + "</h1>\n"

implicit val z: Zone = Zone.open()
val cResult = toCString(html)
cResult

}

@main
def main(text: String) = {
def main(text: String) = Zone {
stdio.printf(generateHtml(text))
}

Expand Down
3 changes: 1 addition & 2 deletions example/scalalib/native/3-multi-module/bar/src/Bar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import scala.scalanative.libc._
import scala.scalanative.unsafe._

object Bar {
def main(args: Array[String]): Unit = {
def main(args: Array[String]): Unit = Zone {
println("Running HelloWorld function")
implicit val z: Zone = Zone.open()
val result = toCString(args(0))
val barValue = HelloWorldBar.stringLength(result)
stdio.printf(c"Bar value: Argument length is %i\n", barValue)
Expand Down
3 changes: 1 addition & 2 deletions example/scalalib/native/3-multi-module/foo/src/Foo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import mainargs.{main, ParserForMethods, arg}
object Foo {
@main
def main(@arg(name = "foo-text") fooText: String,
@arg(name = "bar-text") barText: String): Unit = {
@arg(name = "bar-text") barText: String): Unit = Zone {

implicit val z: Zone = Zone.open()
val cFooText = toCString(fooText)
val cBarText = toCString(barText)

Expand Down
2 changes: 1 addition & 1 deletion example/scalalib/native/4-common-config/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object `package` extends RootModule with ScalaNativeModule {

> ./mill run
...
Foo.value: <h1>hello</h1>
Value: <h1>hello</h1>

> ./mill show releaseMode
"mill.scalanativelib.api.ReleaseMode.ReleaseFast"
Expand Down
10 changes: 4 additions & 6 deletions example/scalalib/native/4-common-config/src/Foo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ import fansi._

object Foo {

def generateHtml(text: String): CString = {
def generateHtml(text: String) (using Zone) = {
val colored = Console.RED + "<h1>" + text + "</h1>" + Console.RESET

implicit val z: Zone = Zone.open()
val cResult = toCString(colored)
cResult
}

val value = generateHtml("hello")

def main(args: Array[String]): Unit = {
stdio.printf(c"Foo.value: %s\n", Foo.value)
def main(args: Array[String]): Unit = Zone {
val value = generateHtml("hello")
stdio.printf(c"Value: %s\n", value)
}
}

0 comments on commit 96c4057

Please sign in to comment.