Questions tagged [primary-constructor]

Primary constructors are a feature you’ll find in a few programming languages, including F#, Scala, and Kotlin. A primary constructor allows the coder to define a constructor for a type and capture the constructor parameters to use in initialization expressions throughout the rest of the type definition.

Primary constructors are a feature you’ll find in a few programming languages, including F#, Scala, and Kotlin. A primary constructor allows the coder to define a constructor for a type and capture the constructor parameters to use in initialization expressions throughout the rest of the type definition.

(http://odetocode.com/blogs/scott/archive/2014/08/14/c-6-0-features-part-ii-primary-constructors.aspx)

11 questions
13
votes
3 answers

Why does Kotlin have two types of constructors?

Kotlin has two types of constructors, primary and secondary. What is the purpose of having two types? In my opinion it makes the code more complicated and inconsistent. If both types of constructors create objects of a class, they are equally…
Jia Li
  • 488
  • 2
  • 8
11
votes
2 answers

Moving away from primary constructors

The C# 6 preview for Visual Studio 2013 supported a primary constructors feature that the team has decided will not make it into the final release. Unfortunately, my team implemented over 200 classes using primary constructors. We're now looking for…
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
4
votes
1 answer

Primary Constructor is not being compiled in C# 6.0

I am trying to write a simple example of the Primary Constructor new feature in C# 6.0 but I am not able to compile it. I have changed the Language version to c# 6.0 but it still doesn't work. public class Person (string fName, string lName) { …
ehh
  • 3,412
  • 7
  • 43
  • 91
3
votes
1 answer

How diamond problem in oops is solved using "shared" strategy?

Diamond problem is handled in some OOPS languages (eg. curl) by having the repeatedly inherited class as "shared"? I want to know how this works. Also, I want to know the role played by primary and secondary constructors in solving the diamond…
2
votes
3 answers

Kotlin. How to declare constant?

I have class, which in primary constructor has some fields: class SomeData(val counter: Int...) { // some logic} I need to create a constant. I usually do it like this: companion object { private const val MAX_VALUE = 1000 } But in my case,…
2
votes
1 answer

Kotlin: How to use custom setters in primary constructor

I don't know how to make it so that when creating an object the values of the parameters "pass through the setters" the closest I've gotten is to duplicate the code, use once in the creation of the object and once again in the setter class User(var…
1
vote
1 answer

How do I pass data from a variable into a constructor using the body of the variable?

I am trying to assign data to my class using the variable that I created. I want to use the variable as an instance of the membership class. Every time I pass any values I get an error. error: no value passed for parameter '_number' var member1 =…
ja233
  • 13
  • 3
0
votes
0 answers

How can I pass value to the primary Constructor of a fragment while going from another fragment using NavGraph

class FragmentA: Fragment() { fun changeFragment(activity: Activity, id: Int, b: Bundle) { findNavController( activity, R.id.nav_fragment_b ).navigate( id, b ) …
0
votes
2 answers

How to access primary constructor parameters inside secondary constructor kotlin

I am learning kotlin and I was reading about constructors: primary and secondary. Here is my question that how should I access primary constructor parameters inside a secondary constructor. I am unable to access but I am not sure why? Why I am not…
Harsh Shah
  • 2,162
  • 2
  • 19
  • 39
0
votes
2 answers

Init a property with a setter in a primary constructor in Kotlin

I have the following code: class Camera : AsyncActiveInputDevice { constructor(inputListener: ((Image) -> Unit)? = null) { this.inputListener = inputListener } override var inputListener: ((Image) -> Unit)? …
Shreck Ye
  • 1,591
  • 2
  • 16
  • 32
0
votes
1 answer

Kotlin primary and secondary constructors during inheritance

I am learning Kotlin and I'm kind of stuck with the constructors. I've written a program to help me understand the concept. The program is as follows: open class Animal(){ var name :String = "noname"; constructor(name:String):this(){ …
rdias002
  • 214
  • 2
  • 9