Class: Steep::Interface::Function::Params::KeywordParams

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/steep/interface/function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#intersection, #union

Constructor Details

#initialize(requireds: {}, optionals: {}, rest: nil) ⇒ KeywordParams

Returns a new instance of KeywordParams.



442
443
444
445
446
# File 'lib/steep/interface/function.rb', line 442

def initialize(requireds: {}, optionals: {}, rest: nil)
  @requireds = requireds
  @optionals = optionals
  @rest = rest
end

Instance Attribute Details

#optionalsObject (readonly)

Returns the value of attribute optionals.



439
440
441
# File 'lib/steep/interface/function.rb', line 439

def optionals
  @optionals
end

#requiredsObject (readonly)

Returns the value of attribute requireds.



438
439
440
# File 'lib/steep/interface/function.rb', line 438

def requireds
  @requireds
end

#restObject (readonly)

Returns the value of attribute rest.



440
441
442
# File 'lib/steep/interface/function.rb', line 440

def rest
  @rest
end

Instance Method Details

#&(other) ⇒ Object

For intersection



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
# File 'lib/steep/interface/function.rb', line 662

def &(other)
  requireds = {} #: Hash[Symbol, AST::Types::t]
  optionals = {} #: Hash[Symbol, AST::Types::t]

  all_keys = Set[] + self.requireds.keys + self.optionals.keys + other.requireds.keys + other.optionals.keys
  all_keys.each do |key|
    case
    when t = self.requireds[key]
      case
      when s = other.requireds[key]
        requireds[key] = intersection(t, s)
      when s = other.optionals[key]
        requireds[key] = intersection(t, s)
      when s = other.rest
        requireds[key] = intersection(t, s)
      else
        return nil
      end
    when t = self.optionals[key]
      case
      when s = other.requireds[key]
        requireds[key] = intersection(t, s)
      when s = other.optionals[key]
        optionals[key] = intersection(t, s)
      when s = other.rest
        optionals[key] = intersection(t, s)
      else
        # nop
      end
    when t = self.rest
      case
      when s = other.requireds[key]
        requireds[key] = intersection(t, s)
      when s = other.optionals[key]
        optionals[key] = intersection(t, s)
      when s = other.rest
        # cannot happen
      else
        # nop
      end
    else
      case
      when s = other.requireds[key]
        return nil
      when s = other.optionals[key]
        # nop
      when s = other.rest
        # nop
      else
        # cannot happen
      end
    end
  end

  rest =
    if self.rest && other.rest
      intersection(self.rest, other.rest)
    else
      nil
    end

  KeywordParams.new(requireds: requireds, optionals: optionals, rest: rest)
end

#+(other) ⇒ Object

For overloading



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
# File 'lib/steep/interface/function.rb', line 533

def +(other)
  requireds = {} #: Hash[Symbol, AST::Types::t]
  optionals = {} #: Hash[Symbol, AST::Types::t]

  all_keys = Set[] + self.requireds.keys + self.optionals.keys + other.requireds.keys + other.optionals.keys
  all_keys.each do |key|
    case
    when t = self.requireds[key]
      case
      when s = other.requireds[key]
        requireds[key] = union(t, s)
      when s = other.optionals[key]
        optionals[key] = union(t, s, null: true)
      when s = other.rest
        optionals[key] = union(t, s, null: true)
      else
        optionals[key] = union(t, null: true)
      end
    when t = self.optionals[key]
      case
      when s = other.requireds[key]
        optionals[key] = union(t, s, null: true)
      when s = other.optionals[key]
        optionals[key] = union(t, s)
      when s = other.rest
        optionals[key] = union(t, s)
      else
        optionals[key] = t
      end
    when t = self.rest
      case
      when s = other.requireds[key]
        optionals[key] = union(t, s, null: true)
      when s = other.optionals[key]
        optionals[key] = union(t, s)
      when s = other.rest
        # cannot happen
      else
        # nop
      end
    else
      case
      when s = other.requireds[key]
        optionals[key] = union(s, null: true)
      when s = other.optionals[key]
        optionals[key] = s
      when s = other.rest
        # nop
      else
        # cannot happen
      end
    end
  end

  if self.rest && other.rest
    rest = union(self.rest, other.rest)
  else
    rest = self.rest || other.rest
  end

  KeywordParams.new(requireds: requireds, optionals: optionals, rest: rest)
end

#==(other) ⇒ Object Also known as: eql?



448
449
450
451
452
453
# File 'lib/steep/interface/function.rb', line 448

def ==(other)
  other.is_a?(KeywordParams) &&
    other.requireds == requireds &&
    other.optionals == optionals &&
    other.rest == rest
end

#each(&block) ⇒ Object



473
474
475
476
477
478
479
480
481
482
483
# File 'lib/steep/interface/function.rb', line 473

def each(&block)
  if block
    requireds.each(&block)
    optionals.each(&block)
    if rest
      yield [nil, rest]
    end
  else
    enum_for :each
  end
end

#each_typeObject



485
486
487
488
489
490
491
492
493
# File 'lib/steep/interface/function.rb', line 485

def each_type
  if block_given?
    each do |_, type|
      yield type
    end
  else
    enum_for :each_type
  end
end

#empty?Boolean

Returns:

  • (Boolean)


469
470
471
# File 'lib/steep/interface/function.rb', line 469

def empty?
  requireds.empty? && optionals.empty? && rest.nil?
end

#hashObject



457
458
459
# File 'lib/steep/interface/function.rb', line 457

def hash
  self.class.hash ^ requireds.hash ^ optionals.hash ^ rest.hash
end

#keywordsObject



526
527
528
# File 'lib/steep/interface/function.rb', line 526

def keywords
  Set[] + requireds.keys + optionals.keys
end

#map_type(&block) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/steep/interface/function.rb', line 495

def map_type(&block)
  if block
    rs = requireds.transform_values(&block)
    os = optionals.transform_values(&block)
    r = rest&.yield_self(&block)

    if requireds == rs && optionals == os && rest == r
      self
    else
      update(requireds: rs, optionals: os, rest: r)
    end
  else
    enum_for(:map_type)
  end
end

#sizeObject



522
523
524
# File 'lib/steep/interface/function.rb', line 522

def size
  requireds.size + optionals.size + (rest ? 1 : 0)
end

#subst(s) ⇒ Object



511
512
513
514
515
516
517
518
519
520
# File 'lib/steep/interface/function.rb', line 511

def subst(s)
  map_type do |type|
    ty = type.subst(s)
    if ty == type
      type
    else
      ty
    end
  end
end

#update(requireds: self.requireds, optionals: self.optionals, rest: self.rest) ⇒ Object



461
462
463
464
465
466
467
# File 'lib/steep/interface/function.rb', line 461

def update(requireds: self.requireds, optionals: self.optionals, rest: self.rest)
  KeywordParams.new(
    requireds: requireds,
    optionals: optionals,
    rest: rest
  )
end

#|(other) ⇒ Object

For union



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
# File 'lib/steep/interface/function.rb', line 597

def |(other)
  requireds = {} #: Hash[Symbol, AST::Types::t]
  optionals = {} #: Hash[Symbol, AST::Types::t]

  all_keys = Set[] + self.requireds.keys + self.optionals.keys + other.requireds.keys + other.optionals.keys
  all_keys.each do |key|
    case
    when t = self.requireds[key]
      case
      when s = other.requireds[key]
        requireds[key] = union(t, s)
      when s = other.optionals[key]
        optionals[key] = union(t, s)
      when s = other.rest
        optionals[key] = union(t, s)
      else
        optionals[key] = t
      end
    when t = self.optionals[key]
      case
      when s = other.requireds[key]
        optionals[key] = union(t, s)
      when s = other.optionals[key]
        optionals[key] = union(t, s)
      when s = other.rest
        optionals[key] = union(t, s)
      else
        optionals[key] = t
      end
    when t = self.rest
      case
      when s = other.requireds[key]
        optionals[key] = union(t, s)
      when s = other.optionals[key]
        optionals[key] = union(t, s)
      when s = other.rest
        # cannot happen
      else
        # nop
      end
    else
      case
      when s = other.requireds[key]
        optionals[key] = s
      when s = other.optionals[key]
        optionals[key] = s
      when s = other.rest
        # nop
      else
        # cannot happen
      end
    end
  end

  rest =
    if self.rest && other.rest
      union(self.rest, other.rest)
    else
      self.rest || other.rest
    end

  KeywordParams.new(requireds: requireds, optionals: optionals, rest: rest)
end