EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Scala Native

By Shalu SharmaShalu Sharma

Home » Software Development » Software Development Tutorials » Scala Tutorial » Scala Native

Scala Native

Introduction to Scala Native

Scala Native acts as a layer and this layer helps us and make the interaction with native language very easy. In scala, we have @native method available. If we defined any method with this annotation i.e. @native that means it should be present in the class, the trait cannot contain @native method in scala. Also if we are defining the body of the method which is marked as @native then it should be type-checked in scala as per the scala documentation written. This can be found under the scala.scalanative.native package.

How does Scala Native work?

As per scala doc scala native class extends some classes and has some liner type also. It contains one constructor without any parameter and we use @native annotation above any method to define them as the native method in scala. Scala scala.scalanative.native this package contained the entire API. Also scala native enables us to interact with the native code that includes other languages and C etc. By using scala active we can also deal with a regular expression and handle most of the cases very well because scala native implemented the java.util.regx. Scala native also provides us with one annotation by the use of it we can link the third-party libraries with the annotation.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The syntax to bind them is as follows:

@native.link("your_lib_name")

In the above syntax, we are using @native.link to bind our libraries with the annotation for this we just need to pas our library name inside the bracket. To use this code or annotation into our scala program we have to include some line of code into our program like, import scala.scalanative.native._ In this package we can find this respective annotation present otherwise it will give us an error.

This scala native also throws some exception which is undefined.

1. ClassCastException

This is the common exception we face while writing code in any language if we make any mistake. Suppose we have one variable which is an integer but we are assigning a value to it as a string. Then in this case we can get a class case exception. So better we deal with them at compile time only.

2. ArithmeticException

This is another common exception that we face while writing the code. Suppose we have a number type variable and we are trying to divide that variable by zero. Then this case we can get this exception.

Popular Course in this category
Scala Programming Training (3 Courses,1Project)3 Online Courses | 9+ Hours | Verifiable Certificate of Completion | Lifetime Validity | 1 Project
4.5 (5,548 ratings)
Course Price

View Course

Related Courses
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)All in One Software Development Bundle (600+ Courses, 50+ projects)

3. OutOfMemoryError

This exception does not come frequently but it comes when we have some memory leaked in our application. Then in this case we may need to increase the memory variable into the file by giving maximum and minimum values.

4. IndexOutOfBoundsException

This exception especially comes in the case when we deal with array and collection and trying to accessing the index more than the length of the array or collection. In such cases, we can get this exception in any language.

5. StackOverflowError

This exception is something very unexpected from the program which is calling recursively or maybe something else.

6. NullPointerException

This is the most common exception we get while programming if we do not handle our objects with care. This often comes when we try to access a null object that means an object with no value or empty.

Scala native also provides us with the provision to work with pointers. But it follows some syntax to define them.

  • Pointer to field- ptr,_N
  • store a filed- ptr. _N=value
  • Store at index- ptr(i)=value
  • store value- !ptr = value
  • pointer to index- ptr+ i
  • load a field- !ptr._N
  • Elements between- ptr1
  • Load value- !ptr
  • Load at index- prt(i)

Scala native also provides us with some linking modes which are given below:

  • release-fast: This mode provides us the fast compilation time which helps us achieve the optimized or we can say reduced runtime performance in scala.
  • debug: This mode provides us with a better development flow. In scala, this is the default native mode.
  • release: This mode is related to the complete release of the application.
  • release-full: This mode provides us with the best runtime performance even better than release-fast.

Some extended class of scala native which are as follows, an extended class available in scala:

  • Annotation
  • StaticAnnotation

Liner type classes available in scala:

  • Any
  • Annotation
  • StaticAnnotation
  • AnyRef

It contains only one constructor which is described as follows:

  • new native(): This is the default or we can say without parameter constructor available.

Scala Native Features

Scala native provides us with many features which are given below:

1. Layer for native code: Scala native provides us with an efficient way to deal and use the native languages code. For this, we can use extern objects in scala. Extern objects are nothing but a wrapper object we can simply use @natve.extern to use any native language code into the program.

Example:

@native.extern

We have to mention this above line of code right before any method that we want to use from any other language.

2. Provide low-level primitives: By this, we can also use pointers in our program to make it more efficient apart from this structs are also there. By the use of these low-level primitives, we can control our application in the exact way we want.

3. Start up: By this feature, we can compile and run our code very fast.

4. It also implemented the Java regex library by the use of it we can easily deal with the regular expression.

5. Scala native also provide us with support to use unsigned integer which is as follows:

  • unsigned.ULong
  • unsigned.UByte
  • unsigned.UInt
  • unsigned.UShort

Example of Scala Native

Given below is the example mentioned :

In this example we are using c language native code into our program to print our first message. This is very simple program we for beginners. But keep in mind we have to run it using native application and do not forget to include the package otherwise it will not work.

Code:

import scalanative.native._, stdlib._
object Main {
def main(args: Array[String]): Unit = {
print("print the message by using the c language method.!!")
// using c code here to print .
fprintf(__stdoutp, c"This is the example from scala native !!")
// it will print the output
}
}

Output:

scala native

Conclusion

Scala Native is just not only the one thing rather it has capability to haled many things like native language support, increase the run time performance of the application, make use of just in time compiler which is responsible for fast compilation also helps in fasten up the development process. It provides support for various java libraries and implements many of them.

Recommended Articles

This is a guide to Scala Native. Here we discuss the introduction to Scala Native, working, features and respective example. You may also have a look at the following articles to learn more –

  1. Scala Singleton
  2. Scala Queue
  3. Scala flatMap
  4. Scala Try Catch

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Scala Tutorial
  • Advanced
    • Constructors in Scala
    • Scala Class Constructor
    • Scala Abstract Class
    • Scala String
    • Scala Collections
    • Scala High Order Functions
    • Scala Date Function
    • Scala Stream
    • Scala Class
    • Scala Function
    • Scala Closure
    • Scala Range
    • Scala Either
    • Scala Random
    • Scala Tuples
    • Scala Partial Function
    • Scala Split
    • Scala Finds
    • Scala List
    • Scala Trait
    • Scala Try Catch
    • Scala Regex
    • Scala Vector
    • Scala Seq
    • Scala Byte
    • Scala ListBuffer
    • Scala ArrayBuffer
    • Scala flatMap
    • Scala HashMap
    • Scala Map Function
    • Scala SortBy
    • Scala Some
    • Scala Generic
    • Scala getOrElse
    • Scala REPL
    • Scala Logging
    • Scala Lambda
    • Scala Queue
    • Scala Set
    • Scala Singleton
    • Scala groupBy
    • Scala reduce
    • Scala filter
    • Scala Option
    • Scala Native
    • Scala DataFrame
    • Scala Anonymous Function
    • Scala Read File
    • Scala Write to File
    • Scala Pattern Matching
    • Scala ClassTag
    • Scala Synchronized
    • Scala zipwithindex
    • Scala Interview Questions
  • Basics
    • What is Scala?
    • Uses of Scala
    • Scala Versions
    • Scala Data Types
    • Scala Companion Object
    • Scala Operators
    • Scala: The Functional Approach
    • Scala Object
    • Scala Implicit
    • Scala IDE
  • Control Flow
    • Scala if else
    • Break in Scala
    • Scala for Loop
    • Scala foreach
    • Scala While Loops

Related Courses

Scala Programming Training Course

Programming Language Course

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Scala Programming Training Course Learn More