0

I am interested to instantiate Duration class from Kotlin.time package provided by kotlin.

my code so far..

...

import kotlin.time

class MyGLRenderer : GLSurfaceView.Renderer {
      override fun onSurfaceCreated(unused: GL10, config: EGLConfig) {
           var tp0:Duration= Duration(10L)
           //cannot acces `<init>:` it is internal in `Duration`          }
      override fun onDrawFrame(unused: GL10) {}
      override fun onSurfaceChanged(unused: GL10, width: Int, height: Int) {}
}

So how can i perform this intention ?

I also tried:

var tp0:Duration= Duration(Duration.)
//to find a getter or setter that can be used to pass the required `long` data type to
// the constructor but no function will return long data type
beginner
  • 27
  • 1
  • 6

2 Answers2

5

You're not supposed to be able to. When a library designer makes a function or constructor hidden from end users like this, it is for a reason. There may be any number of reasons to do this, such as preventing misuse of the class or keeping it flexible to future changes that won't break existing end-user code.

In the case of Duration, the argument for the constructor is an internally defined format where the last bit of the Long is encoding the precision of the value. They are keeping this private because it is an implementation detail. It might change in the future. It's not easy to work with the value in this format, and would be hard to clearly explain in documentation.

For instance, your code above is almost certainly wrong. What do you mean for the 10L to represent? It's weird that you're expressing the duration as a decimal number when the number is encoding two different pieces of information. In its decimal form, the value is completely meaningless.

It is still possible to call the constructor using reflection, but this is not a sensible use for reflection. It is fragile to do this, because the library authors may change the way the constructor is interpreted internally, and it would silently break your code and create new bugs.

If you just want to create a Duration of 10 milliseconds, use 10L.milliseconds.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • actually I wanted to create a two time points. The first one is in the `onSurfaceCreated()` method which will be called once in the game loop. Another one is in the `OnDrawFrame()` which will be called every frame. Because I wanted to calculate the elapsed time between the previous frame and the current frame. The value `10L` is just used to demonstrate the error, not that I wanted to use 10 miliseconds or 10 nanoseconds, sorry about this. – beginner Jun 23 '23 at 23:19
  • You probably don’t need Duration for this at all. You can use Longs that represent milliseconds. – Tenfour04 Jun 24 '23 at 00:38
  • std::chrono::time_point tp0, tp1; float fLastElapsed = 0.0f; onUserUpdate(){ tp0 = std::chrono::system_clock::now(); tp1 = std::chrono::system_clock::now(); } while(true){ tp1 = std::chrono::system_clock::now(); std::chrono::duration elapsedTime = tp1 - tp0; tp0 = tp1; // Our time per frame coefficient float fElapsedTime = elapsedTime.count(); fLastElapsed = fElapsedTime; } – beginner Jun 24 '23 at 01:37
1

Int, Long, and Double types have extension functions <T>.days, <T>.hours, <T>.seconds etc which return Duration.

val d = 10L.milliseconds
println(d)
//10ms
bylazy
  • 1,055
  • 1
  • 5
  • 16