Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 458 Vote(s) - 3.39 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create package-level functions?

#1
I was reading [the Kotlin Reference Guide](

[To see links please register here]

) and one part said:

>In Kotlin, unlike Java or C#, classes do not have static methods. In most cases, it’s recommended to simply use package-level functions instead.

How does one create a package-level function?
Reply

#2
[From the reference:][1]
>All the contents (such as classes and functions) of the source file are contained by the package declared.

So simply by creating a source file like so:

package my.pkg

fun f0()=0
fun f1()=1
We can declare functions named `f0` and `f1` directly visible to the `my.pkg` package. These functions may then be imported and used similarly to classes:

import my.pkg.f0
import my.pkg.f1
Alternatively, using the `*` syntax:

import my.pkg.*


[1]:

[To see links please register here]

Reply

#3
Package-level functions
-----------------------
Package-level functions are also known as top-level functions. They are declared directly inside a file without creating any class for them. They are often utility functions independent of any class:

**UserUtils.kt**

package com.app.user

fun getAllUsers() { }

fun getProfileFor(userId: String) { }

Usage:

import com.app.user.getProfileFor

val userProfile = getProfileFor("34")

You don't need to manually write the `import` statement, just type the function name and the auto-import will do its job.


----------


Package-level functions with a class
------------------------------------

When the functions are somewhat related to a class, define them just above the class, in the same file:

**User.kt**

package com.app.user

fun getAllUsers() { }

fun getProfileFor(userId: String) { }

data class User(val id: String, val name: String)

Usage:

import com.app.user.getAllUsers

val userList = getAllUsers()


----------


`companion object`
------------------

When the functions are closely related to a class, define them inside a `companion object`:

**User.kt**

data class User(val id: String, val name: String) {

companion object {

fun getAll() { }

fun profileFor(userId: String) { }
}
}

Usage:

import com.app.user.User

val userProfile = User.profileFor("34")


----------

That's it!
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through