Skip to content
pannous edited this page Oct 13, 2026 · 1 revision

like

like declares that values of one type may be used wherever another type is expected:

class photo{width:int height:int}
class image{width:int height:int}
image like photo

area(p:photo) := p.width * p.height
area(image{width:3 height:4})   // 12

Duck typing, by design

Warp judges a value by what is done with it. How a place declared photo, a typed parameter area(p:photo) or a typed variable p:photo = …, treats its value depends on how much the compiler knows about that value:

value result
a photo, or a value of a type declared like photo accepted
a value of unknown type: an untyped parameter passed on duck typed: accepted, and judged by its uses. A field it lacks is a loud runtime error: no field height
written data: pic{width:3 height:4} with no class pic, or a map {width:3 height:4} accepted; the name pic is only a label. The fields are checked against photo's: a missing required field or an extra field is a warning (pic{width:3} lacks field height of photo)
a value of a known other type: image{…} compile error that teaches like: image is not a photo; fix: declare image like photo to use it as one
a known non-object: 3, "x" compile error: area needs a photo for parameter p, got 3 (an Int)

So unknown values get the benefit of the doubt, as in Python: untyped code just runs. Two known types never mix silently, even when one has all the fields of the other: that they are interchangeable is a decision, stated once, in one line.

A promise, not a conversion

image like photo changes no value. The image stays an image and is still judged by its uses:

class photo{width:int height:int}
class thumb{width:int}
thumb like photo

keep(p:photo) := p.width
keep(thumb{width:3})            // 3
area(p:photo) := p.width * p.height
area(thumb{width:3})            // error: no field height

Rules

  • like relates two declared types (class, type, struct): banana like photo without class banana is an error.
  • It goes one way: image like photo lets an image stand for a photo, not a photo for an image.
  • A type may be like several types (image like photo; image like page).
  • Likeness chains: thumb like image; image like photo lets a thumb stand for a photo.
  • The declaration may come anywhere in the program, before or after its uses.

Why a keyword

The alternatives were as (reads as a conversion, which this is not) and is (already the type test x is photo). like says what is meant: treat it like one.

See also: matching by type name (to keep a photo: …), traits (class dot{x:int} is Comparable).

Home

Philosophy •

data & code blocks

features

inventions

evaluation

reflection

W0 types, proved

keywords

standard library

use modules & cargo crates, script mode

iteration

switch & pattern matching

warnings & strict

tasks

time & units

html pages

the warp command

examples

todo : bad ideas and open questions

⚠️ specification and progress are out of sync

Clone this wiki locally