Are there any programming languages that use strict evaluation that also have overloaded values?

Haskell supports overloaded values, where a single overloaded value can behave
sort of like a superposition of values each with a different type. For example, here’s a simple type class:

class Truthy t where
  truthy :: Integer -> t

instance Truthy Integer where
  truthy x = x       -- some computation that results in an Integer

instance Truthy Bool where
  truthy x = x /= 0  -- some computation that results in a Bool

Passing a number to truthy returns an overloaded value of type Truthy t => t. This same value can be used as an Integer or a Bool:

Prelude> let y = truthy 17
Prelude> if y then y else 42
17

Note that y is used in a Bool context (the condition) and also in a numeric
context (the then clause, which needs to match the type of the numeric else
clause).

Haskell also uses lazy evaluation, so it isn’t surprising that the two
results of truthy 17 are computed lazily. This means that while an overloaded
value is conceptually a bundle of values of different types, only the instances that are actually used perform computation.

Are there any programming languages that have overloaded values like this but which use strict evaluation?

If so, what are the evaluation semantics of overloaded values like? Are overloaded values:

  • strict (meaning that you have to perform computations for instances you’ll never use)
  • non-strict (which would be inconsistent with non-overloaded values)
  • something else?

7

This is a pretty bog-standard feature of many programming languages that support some kind of operator overloading. However, most languages that do so only support this for built-in types such as booleans, strings, or numeric types.

In Perl, a variable doesn’t generally have an actual type. Instead, it’s coerced to various types however the context requires it. The + operator coerces its operands to numbers, while not extracts a boolean representation. User-defined objects can hook into this coercion process. Example:

package Truthy {
    use Scalar::Util 'looks_like_number';

    # this sets up overloading
    use overload '0+' => &as_num, 'bool' => &as_bool;

    # ignore this constructor code
    sub new {
        my ($class, $value) = @_;
        # this is as close as vanilla Perl gets to type checking ... bleh.
        die "$value must be a number" if not looks_like_number $value;
        return bless $value => $class;
    }

    sub as_num { 
        my ($self) = @_;
        print "called as_numn";
        return $$self;
    }

    sub as_bool {
        my ($self) = @_;
        print "called as_booln";
        return $self != 0;
    }
}

my $y = Truthy->new(17);
print +($y ? $y : 42), "n";

While Perl is a strict language, the coercion methods are only executed whenever the coercion is actually requested. However, the value is not cached in any way.

The Python language is similar, except that the overloading mechanism uses specially named methods such as __str__ and __int__.

One interesting language that actually allows arbitrary coercions even between user-defined types is Scala. We can register “implicit” methods that supply conversions. They are only executed when that conversion is requested, so that these conversions end up having the same semantics as Perl’s or Python’s overloading mechanism.

class Truthy(val x: Int) { }

implicit def asInt(t: Truthy): Int = t.x
implicit def asBool(t: Truthy): Boolean = t.x != 0

val x = new Truthy(17)
val result: Int = if (x) x else 42

You may have noticed that I’m not using type classes (or their equivalents in OOP languages: interfaces, traits) anywhere. Most languages do not support return-type polymorphism. Instead, I expressed your code in terms of coercions which ends up doing the same thing, by very different means.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật