Skip to content

Create struct constructor helper #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kornilova203 opened this issue Aug 10, 2018 · 2 comments
Closed

Create struct constructor helper #155

kornilova203 opened this issue Aug 10, 2018 · 2 comments
Milestone

Comments

@kornilova203
Copy link
Member

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
      }
    }
  }
}
@jonas
Copy link
Member

jonas commented Aug 13, 2018

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
    }
  }
}

@kornilova203
Copy link
Member Author

Yes, I'll do it today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants