scala-native / scala-native-bindgen

Scala Native Binding Generator

Home Page:https://scala-native.github.io/scala-native-bindgen/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Create struct constructor helper

kornilova203 opened this issue · comments

I think struct constructor helper should be in separate constructors object because it will make bindings more readable.
constructors object imports implicits and it makes it possible to reuse setters.

@native.extern
object mylib {
  type struct_point = native.CStruct2[native.CInt, native.CInt]
  
  object implicits {
    implicit class struct_point_ops(val p: native.Ptr[struct_point]) extends AnyVal {
      def x: native.CInt = !p._1
      def x_=(value: native.CInt): Unit = !p._1 = value
      def y: native.CInt = !p._2
      def y_=(value: native.CInt): Unit = !p._2 = value
    }
    def struct_point()(implicit z: native.Zone): native.Ptr[struct_point] = native.alloc[struct_point]
  }

  object constructors {
    import implicits._
    object struct_point {
      def apply(x: native.CInt, y: native.CInt)(implicit z: native.Zone): native.Ptr[struct_point] = {
        val ptr = native.alloc[struct_point]
        ptr.x = x
        ptr.y = y
        ptr
      }
    }
  }
}

Do you still plan to address this for 0.3?

My suggestion is to combine the two struct_x methods.

@native.extern
object mylib {
  type struct_point = native.CStruct2[native.CInt, native.CInt]
  
  object implicits {
    implicit class struct_point_ops(val p: native.Ptr[struct_point]) extends AnyVal {
      def x: native.CInt = !p._1
      def x_=(value: native.CInt): Unit = !p._1 = value
      def y: native.CInt = !p._2
      def y_=(value: native.CInt): Unit = !p._2 = value
    }
  }

  object struct_point {
    import implicits._
    def apply(x: native.CInt, y: native.CInt)(implicit z: native.Zone): native.Ptr[struct_point] = {
      val ptr = native.alloc[struct_point]
      ptr.x = x
      ptr.y = y
      ptr
    }
  }
}

Yes, I'll do it today