Bug in compiler when you init a structure in default value

silver

(Nick Msg) #1

If you init a structure inside of the init of a class you get this error:

Example Code:

   class MyClass {
	
	struct Options {
		var port: Int? = nil
		var pingInterval: UInt16 = 10
		var useTLS = false

		var concretePort: Int {
			return port ?? (useTLS ? 8883 : 1883)
		}

	}
 
	private var options: Options
	private var host: String
	
	init(host: String, options: Options = Options()) {
		self.host = host
		self.options = options
	}
}

Screenshot:


(marc hoffman) #2

btw, pro tip, Cmd-Option-C lets you copy code + error messages, in Fire.


(marc hoffman) #3

i don’t believe we (currently) support non-constant default parameters; in fact, i’m not even sure of that can be supported on Java and .NET, ut i’ll check with the compiler team (and this should err better)


(RemObjects) #4

Thanks, logged as bugs://78125


(Nick Msg) #5

Hello , but anyway the compiler should support all the feature. to compile everything you have in original swift compiler.
It’s shouldn’t be depend on the platform because it’s syntax error.


(marc hoffman) #6

Should, yes, thats why i logged a bug. Carlo tells me that we do indeed support non-const default arguments, and this is an unrelated bug, which will be fixed. that said:

well, some things will, because the compiler cant do stuff that the platform won’t allow. :wink:

To give an (unrelated to this) example, .NET has stricter tules for what a constructor (ie init()) can die before it has called the base constructor. No mater what Swift/Xocde allows you to do, if you want to get a valid .NET executable, you’ll have to live with he limit .NET imposes, there.


(RemObjects) #7

bugs://78125 got closed with status fixed.


(Carlo Kok) #9

Do you have a more complete testcase? I couldn’t reproduce this issue.