comparison src/testdir/test_vim9_class.vim @ 31443:9ae3720f9bd9 v9.0.1054

patch 9.0.1054: object member can't get type from initializer Commit: https://github.com/vim/vim/commit/74e1274edf632b83d2948a2a2d519589eff52997 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Dec 13 21:14:28 2022 +0000 patch 9.0.1054: object member can't get type from initializer Problem: Object member can't get type from initializer. Solution: If there is no type specified try to use the type of the initializer. Check for a valid type.
author Bram Moolenaar <Bram@vim.org>
date Tue, 13 Dec 2022 22:15:03 +0100
parents e572ff386670
children 5804270d6e9b
comparison
equal deleted inserted replaced
31442:2a103f2e659b 31443:9ae3720f9bd9
229 assert_equal("Chris", chris.name) 229 assert_equal("Chris", chris.name)
230 assert_equal(4, chris.age) 230 assert_equal(4, chris.age)
231 assert_equal("none", chris.education) 231 assert_equal("none", chris.education)
232 END 232 END
233 v9.CheckScriptSuccess(lines) 233 v9.CheckScriptSuccess(lines)
234 enddef 234
235 lines =<< trim END
236 vim9script
237 class Person
238 this.name: string
239 this.age: number = 42
240 this.education: string = "unknown"
241
242 def new(this.name, this.age = v:none, this.education = v:none)
243 enddef
244 endclass
245
246 var missing = Person.new()
247 END
248 v9.CheckScriptFailure(lines, 'E119:')
249 enddef
250
251 def Test_class_object_member_inits()
252 var lines =<< trim END
253 vim9script
254 class TextPosition
255 this.lnum: number
256 this.col = 1
257 this.addcol: number = 2
258 endclass
259
260 var pos = TextPosition.new()
261 assert_equal(0, pos.lnum)
262 assert_equal(1, pos.col)
263 assert_equal(2, pos.addcol)
264 END
265 v9.CheckScriptSuccess(lines)
266
267 lines =<< trim END
268 vim9script
269 class TextPosition
270 this.lnum
271 this.col = 1
272 endclass
273 END
274 v9.CheckScriptFailure(lines, 'E1022:')
275
276 lines =<< trim END
277 vim9script
278 class TextPosition
279 this.lnum = v:none
280 this.col = 1
281 endclass
282 END
283 v9.CheckScriptFailure(lines, 'E1330:')
284 enddef
285
235 286
236 287
237 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 288 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker