In Java we have the Class::new syntax for constructor references. I know, there are callable references for methods, but how about constructors? A typical use case for me would be factories.
            Asked
            
        
        
            Active
            
        
            Viewed 2.0k times
        
    145
            
            
        
        Mahozad
        
- 18,032
 - 13
 - 118
 - 133
 
        Kirill Rakhman
        
- 42,195
 - 18
 - 124
 - 148
 
1 Answers
221
            You can get a function instance for a constructor by simply using ::ClassName, as if it were a factory function. 
        gw111zz
        
- 274
 - 2
 - 10
 
        Ilya Ryzhenkov
        
- 11,782
 - 1
 - 40
 - 50
 
- 
                    40Much obliged. Idk if this will help anyone, but to do this with Sealed classes do: `SealedClass::SubClass` – aProperFox Feb 13 '18 at 15:50
 - 
                    1What about references to secondary constructors? – Travis Well May 11 '20 at 20:48
 - 
                    1@TravisWell what would be an equivalent in Java? AFAIK you can't reference specific constructor in Java. The compiler automatically detects which constructor to reference (through Class::new) by inferred (or explicit) argument types – Andrey Jun 01 '20 at 15:47
 - 
                    1@TravisWell you can specify value type for different constructors. For example, `val a: () -> A = ::A` or `val a: (Int) -> A = ::A` – antaki93 Dec 04 '22 at 11:37