java.lang.InstantiationError when factory construction is combined with generics

(Kay Großblotekamp) #1

The following code compiles and should be fine, but it throws a java InstantiationError, when the class needsStuff is instantiated.

import android.content
import android.os
import android.util
import android.view
import android.widget

public protocol Stuff{
    func somefunc(_ x: String)->String

class concretestuff: Stuff{
   public func somefunc(_ x: String) -> String {
		return x+x

public protocol facp{
	func makestuff()->Stuff

class concretefac: facp{
    required init(){}
	public func makestuff() -> Stuff {
		return concretestuff()

class needsStuff<F: facp>{
	private var factory: F
		factory = F()

public class MainActivity: Activity {

	public override func onCreate(_ savedInstanceState: Bundle!) {
		ContentView = R.layout.main
	let t = needsStuff<concretefac>()

This can be circumvented by getting rid of the generic parameter in needsStuff (essentially making the generic parameter a constructor parameter)

Version info:
Microsoft Visual Studio 2015 Shell (Integrated)
Version 14.0.23107.0 D14REL
Microsoft .NET Framework
Version 4.6.00081

Installed Version: IDE Standard

RemObjects Elements
RemObjects Elements (Oxygene, C# and Silver) for .NET, Cocoa and Java.
Copyright 2003-2016 RemObjects Software, LLC. All rights reserved.

RemObjects Elements leverages the LLVM compiler backend:
Copyright © 2003-2016 University of Illinois at Urbana-Champaign. All rights reserved.

RemObjects Everwood
RemObjects Everwood
Copyright RemObjects Software, LLC 2002-2016. All Rights Reserved.

(RemObjects) #2

Thanks, logged as bugs://77299

(Carlo Kok) #3

Unfortunately, due to the nature of java & cocoa this can’t really work. Specifically the F() thing. You’d need to pass a block to be able to call constructors. (the issue here is that java eliminates the generics, so at this point it doesn’t really know what to do here). I’ll see what I can do error wise though.

(Kay Großblotekamp) #4

I get, that this doesn’t work on java, when
mapping protocols to interfaces, since java doesn’t allow to specify constructors in interfaces (similar things might hold for cocoa), but I doubt, that it can’t be made working. The runtime should only be a problem, when trying to export such a construction (or when doing runtime introspection), but otherwise this shouldn’t be much of a problem, should it?