1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
|
// #Conformance #MemberDefinitions #Mutable #ObjectOrientedTypes #Classes #InterfacesAndImplementations #Recursion
(* (c) Microsoft Corporation. All rights reserved *)
// CONTENTS-INDEX-REGEXP = FROM>^//! +<TO
//----------------------------------------------------------------------------
//CONTENTS-START-LINE: HERE=3 SEP=2
// 30. Setup
// 38. Address of incremental class local mutable
// 49. Address of mutable record field (related to above)
// 59. Minor test
// 72. Misc
// 90. Wire prevously
// 114. Wire variations
// 224. Area variations
// 335. Person
// 393. Forms
// 456. typar scoping
// 475. local let rec test
// 506. local let rec test
// 532. override test
// 549. abstract test
// 568. CC tests
// 583. interface
// 591. static expr test
// 599. ctor args stored in fields
// 605. Test cases:
// 621. Finish
//CONTENTS-END-LINE:
//--------------------------------------------------------------------------
#if ALL_IN_ONE
module Core_members_incremental
#endif
//! Setup
let failures = ref []
let report_failure (s : string) =
stderr.Write" NO: "
stderr.WriteLine s
failures := !failures @ [s]
let test (s : string) b =
stderr.Write(s)
if b then stderr.WriteLine " OK"
else report_failure (s)
let check s b1 b2 = test s (b1 = b2)
//! Address of incremental class local mutable
module AddresOfIncrementalClassLocalMutable = begin
open System.Drawing
//open System.Windows.Forms
let rr = new Rectangle() // <-------- value type
type LocalMutableTestA() = class
let mutable mvarA = rr
member x.MethodA() = mvarA.X // <-------- require address of it for arg to getter
end
end
//! Address of mutable record field (related to above)
module AddresOfMutableRecordField = begin
open System.Drawing
//open System.Windows.Forms
type RectR = { mutable field : Rectangle }
let f (x:RectR) = x.field.X <- 10
let boxR = {field = new Rectangle() }
do assert(boxR.field.X=0)
do f boxR
do assert(boxR.field.X=10)
end
//! Minor test
module MinorTest = begin
type A<'a>(x) =
class
let (y:'a) = x
member this.X = y
end
let x1 = new A<string>("abc")
let x2 = new A<int>(3)
let x3 = new A<int64>(3L)
end
//! Misc
module Misc = begin
type 'a Area9aa(x) =
class
let a = (x : 'a)
let f (y:'a) = y
end
type AList(a) = class
let x1 = a + 1
let y2 = x1 + 1
let y3 = 3
member pairs_this.Pair() = x1,y2
end
let a = new AList 12
do assert( a.Pair() = (13,14) )
end
//! Wire prevously
(* accepted *)
module WireOld = begin
[<AbstractClass>]
type 'a wire =
class
abstract Send : 'a -> unit
abstract Listen : ('a -> unit) -> unit
new () = {}
member self.Event = self :> 'a IEvent
interface IEvent<'a> with
member x.Subscribe(handler) = failwith "nyi"
member x.AddHandler(handler) = failwith "nyi"
member x.RemoveHandler(handler) = failwith "nyi"
end
end
let createWire() =
let listeners = ref [] in
{new wire<'a>() with
member __.Send(x) = List.iter (fun f -> f x) !listeners
member __.Listen(f) = listeners := f :: !listeners
}
end
//! Wire variations
module WireVariations = begin
(* Accepted *)
type wire2(z) =
class
let z = z
member this.Send(x) = 1 + z + x
end
(* Accepted *)
type 'a wire3(z) =
class
let listeners = (z:'a)
member this.Send(x:'a) = x
end
(* Accepted *)
type wire4<'a>(z) =
class
let listeners = let z:'a = z in ref ([] : ('a -> unit) list)
member this.Send(x:'a) = List.iter (fun f -> f x) !listeners
member this.Listen(f:'a -> unit) = listeners := f :: !listeners
end
(* Accepted *)
type 'a wire5(z) =
class
let listeners = ref ([] : ('a -> unit) list)
member this.Send(x:'a) = let z:'a = z in List.iter (fun f -> f x) !listeners
member this.Listen(f:'a -> unit) = listeners := f :: !listeners
end
(* Accepted now - fixed tinst missing error *)
type 'a wire6(z) =
class
let mutable listeners = ([] : ('a -> unit) list)
member this.Send(x:'a) = let z:'a = z in List.iter (fun f -> f x) listeners
member this.Listen(f:'a -> unit) = listeners <- f :: listeners
end
(* OK, now this types are asserted equal *)
type 'a wire7(z) =
class
let listeners = 12
let z : 'a = z
member this.SendA(x:int) = this,listeners
member this.SendB(x:int) = this,listeners
end
(* Soundness concern: should uses of let-fields force a member "this" to be ('a wire) typed? *)
type 'a wire8(z) =
class
let z : 'a = z
let listeners = 12
member this.SendA(x:int) = x,z,(this: 'a wire8)
member this.SendB(x:int) = x,z,(this: 'a wire8)
end
(* Accepted *)
type 'a wire9(z) =
class
let mutable listeners = ([] : int list)
let z : 'a = z
member this.Send(x:int) =
let ls : int list = 1 :: 2 :: listeners in
List.map (fun x -> x+1) ls
end
(* Accepted *)
type 'a wire10(z) =
class
let mutable listeners = ([] : int list)
let z : 'a = z
member this.Send(x:int) =
let ls : int list = listeners @ [1] in (* it seems listeners may have the wrong type... *)
List.map (fun f -> f) ls
end
(* Accepted *)
type 'a wire11(z) =
class
let listeners = let z:int = z in ([] : int list)
member this.Send(x:'b) = (* List.iter (fun f -> ()) listeners *)
let xx : _ wire11 = this in
Some (x,List.head listeners)
end
(* Accepted *)
type 'a wire12(z) =
class
let listeners = let z:'a = z in ([] : int list)
member this.Send(x:'b) = (* List.iter (fun f -> ()) listeners *)
Some (this,x,List.head listeners)
end
(* OK, after avoid the value restriction *)
type wire13(z) =
class
member this.Send(x:'b) = (ignore (z:int)); Some (this,x)
end
(* Accepted *)
type 'a wire14(z) =
class
let mutable listeners = let z:int = z in ([] : int list)
member this.Send(x:'a) = (* List.iter (fun f -> ()) listeners *)
listeners
end
end
//! Area variations
#if Portable
#else
module AreaVariations = begin
(* Accepted *)
open System.Drawing
open System.Windows.Forms
open WireOld
type Area1(x) =
class
inherit Panel()
let x = x * 2
end
type Area2(x) =
class
inherit Panel()
let x = x * 2
let mutable Bitmap = new Bitmap(100,100)
let mutable GC = Graphics.FromImage(Bitmap)
let fff (x:Area2) = x.Resize ()
member this.Resize () = Bitmap <- new Bitmap(10,20)
end
type Area3(x) =
class
let basePanel = let x = x:unit in new Panel()
let mutable Bitmap = new Bitmap(100,100)
let mutable GC = Graphics.FromImage(Bitmap)
let fff (x:Area3) = x.Resize ()
member this.Resize () = Bitmap <- new Bitmap(10,20)
end
type Area4 =
class
inherit Panel
member this.X() = base.Dock
end
type Area5(x) =
class
inherit Panel()
let x = 1 + x
(**)
let mutable Bitmap = new Bitmap(100,100)
let mutable GC = Graphics.FromImage(Bitmap) (* GC for bitmap *)
let resizedWire = createWire() : (int*int) wire
let mutable Count = 0
(**)
do base.BackColor <- Color.Black
do base.Dock <- DockStyle.Fill
do base.Cursor <- Cursors.Cross
// do base.Resize.Add(fun arg -> area.resize ()) (* use before defn risk *)
// do base.Paint .Add(fun args -> area.redrawDrawArea args.Graphics) (* use before defn risk *)
(**)
member this.ResizedE = resizedWire.Event
(**)
member this.Resize () =
let ww,hh = base.Width,base.Height in
if ww>0 && hh>0 then (
Bitmap <- new Bitmap(ww,hh);
GC <- Graphics.FromImage(Bitmap);
resizedWire.Send((ww,hh))
)
(**)
// TODO: need to be able to call this.PriorMethod without this.qualification
// do base.Resize.Add(fun arg -> Resize ()) (* no risk *)
(**)
member this.RedrawDrawArea (gc:Graphics) = gc.DrawImage(Bitmap,new Point(0,0))
// member this.Refresh() = using (base.CreateGraphics()) (fun gc -> RedrawDrawArea gc)
member this.Refresh2() = using (base.CreateGraphics()) (fun gc -> this.RedrawDrawArea gc)
(**)
// do base.Paint.Add(fun args -> RedrawDrawArea args.Graphics) (* no risk *)
// do base.Paint.Add(fun args -> <impliedThisVariable>.RedrawDrawArea args.Graphics) (* no risk *)
end
type Area5b(x) as self =
class
inherit Panel()
let x = 1 + x
(**)
let mutable Bitmap = new Bitmap(100,100)
let mutable GC = Graphics.FromImage(Bitmap) (* GC for bitmap *)
let resizedWire = createWire() : (int*int) wire
let mutable Count = 0
(**)
do self.BackColor <- Color.Black
do self.Dock <- DockStyle.Fill
do self.Cursor <- Cursors.Cross
// do self.Resize.Add(fun arg -> area.resize ()) (* use before defn risk *)
// do self.Paint .Add(fun args -> area.redrawDrawArea args.Graphics) (* use before defn risk *)
(**)
member this.ResizedE = resizedWire.Event
(**)
member this.Resize () =
let ww,hh = self.Width,self.Height in
if ww>0 && hh>0 then (
Bitmap <- new Bitmap(ww,hh);
GC <- Graphics.FromImage(Bitmap);
resizedWire.Send((ww,hh))
)
(**)
// TODO: need to be able to call this.PriorMethod without this.qualification
// do self.Resize.Add(fun arg -> Resize ()) (* no risk *)
(**)
member this.RedrawDrawArea (gc:Graphics) = gc.DrawImage(Bitmap,new Point(0,0))
// member this.Refresh() = using (self.CreateGraphics()) (fun gc -> RedrawDrawArea gc)
member this.Refresh2() = using (self.CreateGraphics()) (fun gc -> this.RedrawDrawArea gc)
(**)
// do self.Paint.Add(fun args -> RedrawDrawArea args.Graphics) (* no risk *)
// do self.Paint.Add(fun args -> <impliedThisVariable>.RedrawDrawArea args.Graphics) (* no risk *)
end
type Area6(x) =
class
inherit Panel()
let x = 1 + x
do base.BackColor <- Color.Black
end
type Area7(x) =
class
inherit Panel()
let a,b = x : int * int
do base.BackColor <- Color.Black
end
open System.Drawing
open System.Windows.Forms
type 'a Area8(x) =
class
inherit Panel()
let a,b = (x : int * int)
do base.BackColor <- Color.Black
let f (y:'a) = y
let xx = a
end
type 'a Area9(x) =
class
let a = (x : 'a)
// let f (y:'a) = y
end
type 'a Area10 =
class
val f : 'a -> 'a
new (x:int) = { f = fun x -> x}
end
end
#endif
//! Person
(* Scala person example *)
module ScalaPersonExample = begin
type Person1(firstLastSpouse) =
class
let firstName,lastName,spouse = firstLastSpouse
member x.FirstName = firstName : string
member x.LastName = lastName : string
member x.Spouse = spouse : Person1 option
member x.Introduction() =
"Hi, my name is " + firstName + " " + lastName +
(match spouse with
| None -> "."
| Some spouse -> " and this is my spouse, " + spouse.FirstName + " " + spouse.LastName + ".")
// TODO: the implicit ctor is not in scope for defining alt constructors.
// new (f,l) = new Person1(f,l,None)
end
let pA = new Person1(("bob" ,"smith",None))
let pB = new Person1(("jill","smith",Some pA))
end
//! Forms
#if Portable
#else
module Forms1 = begin
open System.Drawing
open System.Windows.Forms
type DrawPanel(w) as x =
class
inherit Panel()
do base.Width <- w
do base.Height <- w
do base.BackColor <- Color.Black
let mutable BM = new Bitmap(w,w)
let refresh() = using (x.CreateGraphics()) (fun gc -> gc.DrawImage(BM,new Point(0,0)))
let GC' = Graphics.FromImage(BM)
do x.Paint.Add(fun _ -> refresh())
(*external*)
member this.Redraw() = refresh()
member this.GC = GC'
end
end
module Forms2 = begin
open System.Drawing
open System.Windows.Forms
type DrawPanel2 (w,h) as x =
class
inherit Panel()
do base.Width <- w
do base.Height <- h
do base.BackColor <- Color.DarkBlue
let mutable BM = new Bitmap(w,w)
let refresh() = using (x.CreateGraphics()) (fun gc -> gc.DrawImage(BM,new Point(0,0)))
let GC' = Graphics.FromImage(BM)
let uuu = ()
let u2 = uuu,uuu
do x.Paint.Add(fun _ -> refresh())
(*external*)
member this.Redraw() = refresh()
member this.GC = GC'
end
let form = new Form(Visible=true,AutoScroll=true)
let dp = new DrawPanel2(800,400)
do form.Controls.Add(dp)
do dp.GC.DrawLine(Pens.White,10,20,30,40)
do dp.Redraw()
end
#endif
module Regression1 = begin
(* Regression test: local vals of unit type are not given field storage (even if mutable) *)
type UnitTestA2 (x,y) =
class
do printf "TestA %d\n" (x*y)
let u = ()
let mutable mu = ()
let mutable kept = false
member this.Unlikely() = mu <- (kept <- true; (* ensure effect is not optimised away *)
printf "Boo\n"
)
member this.Kept with get() = kept
end
let x = new UnitTestA2(1,2)
do x.Unlikely()
do assert(x.Kept = true)
end
//! typar scoping
module TyparScopeChecks = begin
(* typar scoping checks 1 *)
type ('a,'b) classB1(aa,bb) =
class
let a = aa : 'a
let b = bb : 'b
let f (aaa:'a) (bbb:'b) = aaa,bbb
end
(* typar scoping checks *)
type ('a,'b) classB2(aa:'a,bb:'b) =
class
let a = aa
let b = bb
let f (aaa:'a) (bbb:'b) = aaa,bbb
end
end
module LocalLetRecTests = begin
//! local let rec test
(* let rec tests *)
type LetRecClassA1 (a,b) =
class
let a,b = a,b
let x = a*3 + b*2
let rec fact n = if n=1 then 1 else n * fact (n-1)
member this.Fa() = fact a
member this.Fb() = fact b
end
let lrCA1 = new LetRecClassA1(3,4)
let xa = lrCA1.Fa()
let xb = lrCA1.Fb()
(* let rec tests *)
type LetRecClassA2 () =
class
let rec odd n = printf "odd %d?\n" n; if n=0 then false else not(even (n-1))
and even n = printf "even %d?\n" n; if n=0 then true else not(odd (n-1))
member this.Even(x) = even x
member this.Odd(x) = odd x
end
let lrCA2 = new LetRecClassA2()
let ex2 = lrCA2.Even(2)
let ex3 = lrCA2.Odd(3)
let ox2 = lrCA2.Odd(2)
let ox3 = lrCA2.Even(3)
//! local let rec test
type nats = HD of int * (unit -> nats)
let rec ns = HD (2,(fun () -> HD (3,(fun () -> ns))))
let rec take n (HD (x,xf)) = if n=0 then [] else x :: take (n-1) (xf())
type LetRecClassA3 () =
class
let rec ns = HD (2,(fun () -> HD (3,(fun () -> ns))))
let rec xs = 1::2::3::xs (* WHY IS THIS ACCEPT HERE, BUT NOT AT THE TOP LEVEL??? *)
member this.NS = ns
member this.XS = xs
end
let lrCA3 = new LetRecClassA3()
let ns123 = lrCA3.NS
let xs123 = lrCA3.XS
type LetRecClassA4 () =
class
let rec xs = 1::2::3::xs (* WHY IS THIS ACCEPT HERE, BUT NOT AT THE TOP LEVEL??? *)
member this.XS = xs
end
let lrCA4 = new LetRecClassA4()
let error123 = lrCA4.XS
end
//! override test
module OverrideTest1 = begin
[<AbstractClass>]
type AbstractClass =
class
new() = {}
abstract MA : int -> int
abstract MB : int -> int
end
type OverrideClassA1 () =
class
inherit AbstractClass()
override this.MA(x) = x+1
override this.MB(x) = x+2
end
end
module OverrideTest2 = begin
//! abstract test
[<AbstractClass>]
type ImplicitAbstractClass() =
class
abstract MA : int -> int
abstract MB : int -> int
end
type OverrideClassA2 () =
class
inherit ImplicitAbstractClass()
override this.MA(x) = x+1
override this.MB(x) = x+2
end
let oca2 = new OverrideClassA2()
do assert(oca2.MA(1)=2)
do assert(oca2.MB(1)=3)
end
module ConstructionTests = begin
//! CC tests
type CC (x) =
class
let mutable z = x+1
let f = fun () -> z
member a.F() = f()
member a.Set x = z <- x
end
let cc = new CC(1)
cc.F()
cc.Set(20)
cc.F()
//! interface
type CCCCCCCC() = class
interface System.IDisposable with
member x.Dispose() = ()
end
end
end
module StaticMemberScopeTest = begin
//! static expr test
type StaticTestA1(argA,argB) = class
let locval = 1 + argA
let mutable locmut = 2 + argB
static member M = 12 (* can not use: locval + locmut + argA *)
end
end
module ConstructorArgTests = begin
//! ctor args stored in fields
type CtorArgClassA1(argA,argB) = class
member x.M = argA + argB*2
end
end
module SelfReferenceTests = begin
type SelfReferences(a,b) as self = class
let f() = self.M
let ab = a*b+2
let x =
let ok =
try
let x = self.M in
printf "Error: self reference got through - should catch these\n";
false (* not ok here *)
with :? System.InvalidOperationException ->
printf "Self reference during construction threw exception, as required\n";
true (* ok, got exn *)
in
assert(ok);
ok
member self.M = (ab : int)
member self.P = f()
end
let sr = new SelfReferences(1,2)
end
//! Bug: 878 - type unsoundness - implicit constructor members generalising "free-imperative" typars from let-bindings
(* must be -ve test
type C() = class
let t = ref (Map.empty<string,_>)
let f x = (!t).Add("3",x)
member x.M() = !t
end
*)
module MixedRecursiveTypeDefinitions = begin
type ClassType<'a>(x:'a) =
class
member self.X = x
end
and RecordType =
{ field1 : int;
field2 : ClassType<int> }
with
member self.GetField() = self.field2
end
and UnionType =
| Case1 of string * AbbrevType1
| Case2 of string * AbbrevType2
and AbbrevType1 = ClassType<int>
and AbbrevType2 = ClassType<string>
and AnotherClassType<'a>(x:'a) =
class
// member self.X = x
interface InterfaceType<'a> with
member self.X = x
end
end
and InterfaceType<'a> =
interface
abstract X : 'a
end
end
//! Test cases:
(*
[ ] - self-references - allowed and trapped
[ ] - direct calls to most derrived base override
[ ] - calls on "super" object - via self and super vars
*)
//! Finish
#if ALL_IN_ONE
let RUN() = !failures
#else
let aa =
match !failures with
| [] ->
stdout.WriteLine "Test Passed"
System.IO.File.WriteAllText("test.ok","ok")
exit 0
| _ ->
stdout.WriteLine "Test Failed"
exit 1
#endif
|