1

I am setting up the interop layer for this ThreeJS class and the constructor of the class takes in a object which is used to set the properties.

//PointCloudMaterial.js    
THREE.PointCloudMaterial = function ( parameters ) {
    THREE.Material.call( this );
    this.color = new THREE.Color( 0xffffff );
    this.map = null;
    this.size = 1;
    this.sizeAttenuation = true;
    this.vertexColors = THREE.NoColors;
    this.fog = true;
    this.setValues( parameters );
};

Below is what I would like to be able to do in Kotlin, is it possible to use anomalous objects in should a fashion? I was originally thinking of creating an object equivalent to the possible perimeters to pass in, problem with that is it would override the current values which is not something I want.

//Interop Layer
native("THREE.PointCloudMaterial")
public class PointCloudMaterial(parameters: object) { } //This doesn't compile "Type Expected"

//Example usage
var sizeObject = object {
     var size: Double = size
}
PointCloudMaterial(sizeObject);

1 Answer 1

2

Type safe solution may be look like:

native 
val <T> undefined: T = noImpl

class PointCloudMaterialParameters (
   val color: Int = undefined,
   val opacity: Double = undefined,
   //val map: THREE.Texture = undefined,
   val size: Double = undefined,
   //val blending: THREE.NormalBlending = undefined,
   val depthTest: Boolean = undefined,
   val depthWrite: Boolean = undefined,
   val vertexColors: Boolean = undefined,
   val fog: Boolean = undefined
)

fun main(args : Array<String>) {
  println(PointCloudMaterialParameters(size = 2.0))
}

native("THREE.PointCloudMaterial")
public class PointCloudMaterial(parameters: PointCloudMaterialParameters)

//Example usage
PointCloudMaterial(PointCloudMaterialParameters(size = 2.0))

Another solution which is shorter, but not type safe is:

native("THREE.PointCloudMaterial")
public class PointCloudMaterial(parameters: Any)

//Example usage
PointCloudMaterial(object { val size = 2.0 })

P.S. We will try to simplify this case in the future.

Sign up to request clarification or add additional context in comments.

2 Comments

The first isn't really type safe since it allows the construction of a PointCloudMaterialParameters with undefined for non-null values. In this case, I believe all of the parameters to PointCloudMaterialParameters should all be nullable.
Though, marking them as nullable is also not correct since the Kotlin static analyzer will see them all as having a value, which means ?. and :? operators won't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.